Is it possible to rename a Hashmap key?

hashMap.put("New_Key", hashMap.remove("Old_Key"));

This will do what you want but, you will notice that the location of the key has changed.


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);

Assign the value of the key, which need to be renamed, to an new key. And remove the old key.

hashMap.put("New_Key", hashMap.get("Old_Key"));
hashMap.remove("Old_Key");

Tags:

Java

Hashmap