Is it possible to rename a Hashmap key?

Ikes picture Ikes · May 26, 2012 · Viewed 87.1k times · Source

I'm looking for a way to rename a Hashmap key, but i don't know if it's possible in Java.

Answer

Alexis Pigeon picture Alexis Pigeon · May 26, 2012

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