Skip to content

LinkedHashMap/Set does not behave as expected #1599

@danieldietrich

Description

@danieldietrich

We expect a LinkedHashMap to behave like this:

// pair to replace does not exist
   ((1, "a"), (2, "b")).replace((0, "?"), (0, "!"))
== ((1, "a"), (2, "b"))

// pair to replace does not exist, even if key exists
   ((1, "a"), (2, "b")).replace((2, "?"), (2, "!"))
== ((1, "a"), (2, "b"))

// order is preserved when replacing pair with same key and different value
   ((1, "a"), (2, "b"), (3, "c")).replace((2, "b"), (2, "B"))
== ((1, "a"), (2, "B"), (3, "c"))

// order is preserved when replacing pair with different key and different value
   ((1, "a"), (2, "b"), (3, "c")).replace((2, "b"), (4, "B"))
== ((1, "a"), (4, "B"), (3, "c"))

// order is preserved and other pair with existing key is removed
   ((1, "a"), (2, "b"), (3, "c"), (4, "d"), (5, "e")).replace((2, "b"), (4, "B"))
== ((1, "a"), (4, "B"), (3, "c"), (5, "e"))

// replacing pair with identity
   ((1, "a"), (2, "b"), (3, "c")).replace((2, "b"), (2, "b"))
== ((1, "a"), (2, "b"), (3, "c"))

We expect a LinkedHashSet to behave like this:

// element to replace does not exist
   (1, 2, 3).replace(4, 0)
== (1, 2, 3)

// order is preserved when replacing element
   (1, 2, 3).replace(2, 0)
== (1, 0, 3)

// order is preserved and other pair with existing key is removed
   (1, 2, 3, 4, 5).replace(2, 4)
== (1, 4, 3, 5)

// replacing element with identity
   (1, 2, 3).replace(2, 2)
== (1, 2, 3)

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions