HashMap<String,String> data = new HashMap<>();
data.put("AAA", "CCC");
I want to replace AAA with BBB during run-time without adding a new key into the map.
Is this possible?
There is no* way to edit existing key, the only way is to remove the old one and add a new key. The most elegant way to do it (IMHO) is this:
map.put( "newKey", map.remove( "oldKey" ) );
*: As @Michael Ziluck say in his comment and answer, you could technically do it with reflection, but as it destroy the integrity of HashTable, you should only do it at your own risk
Just delete old pair. Make a new pair with the new key and do insert.
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