Skip to content
Advertisement

How to replace whole key/value pair from a LinkedHashmap at the same replaced position

I am trying to replace whole key/value pair from my LinkedHashMap but seems it is not working. I am getting concurrentmodificationexception. Is there a way to replace whole key/value pair for a key at the same position without any major changes.

I tried to do the following:

JavaScript

Advertisement

Answer

As far as I have understood, you want to stick with LinkedHashMap. If you will try to add new data (change the structure of the LinkedHashMap) while iterating over this, you will get ConcurrentModificationException.

Below code might fulfill your requirement :

JavaScript

Output :

Test Map before :: {1=One, 2=Two, 3=Three, 4=Four}
Test Map after :: {1=One, 2=Two, 44=FourFour, 4=Four}

Code for add element at specific index in LinkedHashMap from HERE

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement