Java LinkedHashMap replace key

Is there a way to replace a key using put()LinkedHashMap without losing the order in which the key was inserted?

+4
source share
2 answers

If you used LinkedHashMap, I do not think that there is a built-in method to achieve your goal. You might want to choose another (or create your own) data structure.

If you need to do this on a related hashmap, you can create a new LinkedHashMap, iterate over the old one and put it in a new one when your target entry appears, create a new entry with a different key, insert a new entry into the map.

+3
source

.

Map<String, String> map = new LinkedHashMap<String, String>();
map.put("foo", "bar");
map.put("blah", "baz");
System.out.println(map);
map.put("foo", "foo");
System.out.println(map);

{foo=bar, blah=baz}
{foo=foo, blah=baz}

Edit

"" , .

, LinkedHashMap, , remove put.

+3

All Articles