首页 > 解决方案 > Update the Key of a Map

问题描述

I have a Linked Hash Map as follows :

Map <Object, Object> test ;

With:

The keys are Strings with 4 chars max , like this :

1234 -> value1 
2 -> value2
342 -> value3
40 -> value4

And I want to update the values of the keys filling the ones that has less than 4 chars , with zeros '0'. For example :

1234 -> value1
2000-> value2
3420 -> value 3
4000 -> value 4

I've tried

  test.forEach((key, value) -> {
                Object storedValue = value;
                Object normalizedKey = StringUtils.rightPad(key.toString(),4,'0');
                test.remove(key);
                test.put(normalizedKey,storedValue);
        });

But its giving me ConcurrentMOdificationException.

How can I achieve my goal ?

标签: javalinkedhashmap

解决方案


推荐阅读