contain-rs / linked-hash-map

A HashMap wrapper that holds key-value pairs in insertion order
https://contain-rs.github.io/linked-hash-map/linked_hash_map/
Apache License 2.0
169 stars 60 forks source link

Inserting a existing key will change the order of keys #111

Closed ptazithos closed 2 years ago

ptazithos commented 2 years ago

Description:

Inserting a exising key will change the order of keys.

let mut map = LinkedHashMap::new();
map.insert("a",0);
map.insert("b",1);
map.insert("c",2);

Now, the order of the keys is "a", "b" and "c". Then we update the value of "b" by insert method:

map.insert("b",0);

The order of the keys changes to "a", "c" and "b". Intutively, I think the the behavior of map.insert("b",0); should be equivalent to map["b"] = 0; except the way they handle exceptions. Is this a feature or a bug?

probablykasper commented 2 years ago

I would say it's expected. Values are stored in insertion order, so when I call .insert() I'd expect it to be ordered last

ptazithos commented 2 years ago

I would say it's expected. Values are stored in insertion order, so when I call .insert() I'd expect it to be ordered last

That makes sense. Thank you!