0

I want to change the key of a specific origin key name of a map, and I want to find a way to modify it just in place, so that it will not copy things around while doing it. I already know that I can only delete a entry of the map through iterate using only a Iterator. But when I put things back to the map while iterating the map. It can't be done. Anyway, I will put my sample code below, hope some one could give me a hint of doing this.

public class TestMapModify {
    public static void main(String[] argvs){
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("a",1);
        m.put("b",2);
        for(Iterator<Map.Entry<String, Object>> it = m.entrySet().iterator();it.hasNext();){
            Map.Entry<String, Object> entry = it.next();
            if(entry.getKey().equals("a")){
                m.put("c", entry.getValue());
                it.remove();
            }
        }
        System.out.println(m);
    }
}

On the above code, I want to change the key "a" to "c" and leaving it's value unmodified. But this code snippet will give a exception:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.remove(HashMap.java:944)
    at me.armnotstrong.TestMapModify.main(TestMapModify.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

EDIT

I am aware that what cause this problem, but I want to find the "best" or alternative way to do that.

armnotstrong
  • 8,605
  • 16
  • 65
  • 130
  • 4
    Any modifications except through the iterator will invalidate the iterator. Collect the entries to be relocated to a new map, after iteration is finished use putAll() to merge the changes. – Durandal Apr 21 '15 at 10:21
  • Removing a value from the map, while iterating over it will cause this exception – Lucky Apr 21 '15 at 10:23
  • Use a https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ? – Alan Barrows Apr 21 '15 at 10:24
  • possible duplicate of [Java - Exception in thread "main" java.util.ConcurrentModificationException](http://stackoverflow.com/questions/26418325/java-exception-in-thread-main-java-util-concurrentmodificationexception) – Lucky Apr 21 '15 at 10:25
  • @Lucky I am aware that this may case a exception as the question you refered, but I only want to find a way to do that :-D – armnotstrong Apr 21 '15 at 10:27
  • @Durandal Sounds like a alternative idea ! – armnotstrong Apr 21 '15 at 10:28
  • @Durandal that would be a good answer – BeyelerStudios Apr 21 '15 at 10:33

1 Answers1

1

@Durandal has already answered this answer. I didn't read it earlier.

You can put new values into another map and then add it to existing map after iteration.

public class TestMapModify {
    public static void main(String[] argvs) {
        Map<String, Object> m = new HashMap<String, Object>();
        Map<String, Object> anotherMap = new HashMap<String, Object>();

        m.put("a", 1);
        m.put("b", 2);
        for (Iterator<Map.Entry<String, Object>> it = m.entrySet().iterator(); it.hasNext();) {
            Map.Entry<String, Object> entry = it.next();
            if (entry.getKey().equals("a")) {
                anotherMap.put("c", entry.getValue());
                it.remove();
            }
        }
        m.putAll(anotherMap);
        System.out.println(m);
    }
}
Naman Gala
  • 4,670
  • 1
  • 21
  • 55