I'm looking for a way to rename a Hashmap key, but i don't know if it's possible in Java.
HashMap replace() Method in Java The replace(K key, V value) method of HashMap replaces an entry for the specified key. It returns replaced value. It returns null if the map does not contain an entry for the specified key.
hashmap. put(key, hashmap. get(key) + 1); The method put will replace the value of an existing key and will create it if doesn't exist.
Well, you can't do it by iterating over the set of values in the Map (as you are doing now), because if you do that then you have no reference to the keys, and if you have no reference to the keys, then you can't update the entries in the map, because you have no way of finding out which key was associated with the ...
HashMap stores key, value pairs and it does not allow duplicate keys.
Try to remove the element and put it again with the new name. Assuming the keys in your map are String
, it could be achieved that way:
Object obj = map.remove("oldKey"); map.put("newKey", obj);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With