I'm looking for a way to rename a Hashmap key, but i don't know if it's possible in Java.
-
12Gosh, 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. – Maarten Bodewes May 26 '12 at 14:04
-
6Please 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). – Donal Fellows May 26 '12 at 14:12
9 Answers
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);

- 7,423
- 11
- 39
- 44
-
81+1. And simplest to read is `map.put( "newKey", map.remove( "oldKey" ) );` and provided contains `oldKey` – Ravinder Reddy May 26 '12 at 14:22
-
15As 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). – Alexis Pigeon May 26 '12 at 15:00
-
3For 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` to `put` without casting it or declaring it as another type, but of course passing the result of `remove` directly works. – Samuel Edwin Ward Jun 11 '15 at 16:12
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.

- 1,094
- 1
- 14
- 20
-
@Vins revisit the documentations please :D, remove() will return the object, check this https://www.tutorialspoint.com/java/util/hashmap_remove.htm – Mohamed Zakaria El-Zoghbi Apr 09 '17 at 08:13
-
1
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");

- 1,099
- 2
- 15
- 20
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
-
this is the best answer in my opinion since it's *not* possible to rename a key. – Wolfson Oct 02 '20 at 07:36
You don't rename a hashmap key, you have to insert a new entry with the new key and delete the old one.

- 33,649
- 14
- 85
- 108
I'd argue the essence of hasmap keys are for index access purposes and nothing more but here's a hack: making a key-wrapper class around the value of the key so the key-wrapper object becomes the hashmap key for index access, so you may access and change key-wrapper object's value for your specific needs:
public class KeyWrapper<T>{
private T key;
public KeyWrapper(T key){
this.key=key;
}
public void rename(T newkey){
this.key=newkey;
}
}
Example
HashMap<KeyWrapper,String> hashmap=new HashMap<>();
KeyWrapper key=new KeyWrapper("cool-key");
hashmap.put(key,"value");
key.rename("cool-key-renamed");
Though you could also have a non existing key be able to get the value of an existing key from the hashmap but I fear it might be criminal, anyways:
public class KeyWrapper<T>{
private T key;
public KeyWrapper(T key){
this.key=key;
}
@Override
public boolean equals(Object o) {
return hashCode()==o.hashCode();
}
@Override
public int hashCode() {
int hash=((String)key).length();//however you want your hash to be computed such that two different objects may share the same at some point
return hash;
}
}
Example
HashMap<KeyWrapper,String> hashmap=new HashMap<>();
KeyWrapper cool_key=new KeyWrapper("cool-key");
KeyWrapper fake_key=new KeyWrapper("fake-key");
hashmap.put(cool_key,"cool-value");
System.out.println("I don't believe it but its: "+hashmap.containsKey(fake_key)+" OMG!!!");

- 821
- 1
- 8
- 20
In my case had a map containing not real key -> real keys, so I had to replace the not reals with the reals in my map (the idea is like others)
getFriendlyFieldsMapping().forEach((friendlyKey, realKey) ->
if (map.containsKey(friendlyKey))
map.put(realKey, map.remove(friendlyKey))
);

- 14,473
- 9
- 96
- 92
You can, if instead of Java native HashMap you'll use a Bimap.
It's not the traditional Map implementation, and you need to make sure it's suits your needs.
A bimap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys. This constraint enables bimaps to support an "inverse view", which is another bimap containing the same entries as this bimap but with reversed keys and values.
Using a bimap, you can inverse the view and replace the key.
Checkout both Apache Commons BidiMap and Guava BiMap.

- 1,008
- 1
- 14
- 31