Is it possible to rename a Hashmap key?
109,396
Solution 1
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);
Solution 2
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.
Solution 3
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");
Solution 4
You cannot rename/modify the hashmap key
once added.
Only way is to delete/remove the key
and insert with new key
and value
pair.
Reason : In hashmap internal implementation the Hashmap key
modifier marked as final
.
static class Entry<K ,V> implements Map.Entry<K ,V>
{
final K key;
V value;
Entry<K ,V> next;
final int hash;
...//More code goes here
}
For Reference : HashMap
Solution 5
You don't rename a hashmap key, you have to insert a new entry with the new key and delete the old one.
Related videos on Youtube
Author by
Ikes
Updated on October 03, 2020Comments
-
Ikes almost 4 years
I'm looking for a way to rename a Hashmap key, but i don't know if it's possible in Java.
-
Maarten Bodewes about 12 yearsGosh, I hope not. Deleting and reentering the key/value pair seems like the way to go. Note that you normally just handle references in the map itself anyway.
-
Donal Fellows about 12 yearsPlease don't modify the key of a hash entry! If you're lucky, you'll change it to something with the same hashcode and you'll just go somewhat crazy trying to figure out what happened; if you're unlucky, you'll end up with an entry that can't be found (well, not until the next rebuild of the whole table). Remove/reinsert is much saner, and should be pretty cheap (it's all references, after all).
-
-
Ravinder Reddy about 12 years+1. And simplest to read is
map.put( "newKey", map.remove( "oldKey" ) );
and provided containsoldKey
-
Alexis Pigeon about 12 yearsAs far as readability is concerned, I quite disagree, I personnally prefer to see clearly that an object is removed from the map, and then added. And since the OP seems to be pretty new to Java, I decided to put it that way. For the sake of performance however, your version is of course prefered (since I don't think the compiler will optimize my version your way).
-
Samuel Edwin Ward about 9 yearsFor javac 1.8.0_45, the one-line version is two bytecodes shorter, which surprised me. More annoyingly with generics you can't pass
obj
toput
without casting it or declaring it as another type, but of course passing the result ofremove
directly works. -
Mohamed Zakaria El-Zoghbi about 7 years@Vins revisit the documentations please :D, remove() will return the object, check this tutorialspoint.com/java/util/hashmap_remove.htm
-
Vins about 7 yearsmy bad, I have removed my comment and voted for it. Sorry about that.
-
Wolfson almost 4 yearsthis is the best answer in my opinion since it's not possible to rename a key.