Java Map deleting elements during traversal

Posted by mazman on Sat, 04 Apr 2020 06:54:03 +0200

map traversal judgment filter deletion

  • If you use the put, remove, or clear methods for a map (for example, map.remove), the iterator is no longer legal (and a ConcurrentModificationException will be thrown if you use the iterator later)
  • When the Iterator.remove method causes the map to change, it updates the cursor to synchronize the change.

Refer to jdk document description:

The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Conclusion: iterative deletion should be used

Extension

For other collections such as list, iterative deletion is also required for deletion during traversal

Test demo

private static Map<Integer, String> map=new HashMap<Integer,String>();

public static void iterTest(){
    map.put(1,"one");
    map.put(2,"two");
    map.put(3,"three");
    map.put(4,"four");
    map.put(5,"five");
    map.put(6,"six");
    map.put(7,"seven");
    map.put(8,"eight");
    map.put(5,"five");
    map.put(9,"nine");
    map.put(10,"ten");

    Iterator<Map.Entry<Integer, String>> iter = map.entrySet().iterator();
    while(iter.hasNext()){
        Map.Entry<Integer, String> entry=iter.next();
        int key=entry.getKey();
        if(key%2==1){
            System.out.println("delete this: "+key+" = "+key);
            //map.put(key, "odd"); / / ConcurrentModificationException
            //map.remove(key);      //ConcurrentModificationException
            iter.remove();        //OK
        }
    }
    //Traverses the current map; this new for loop cannot modify the map content because it does not pass through the iterator.
    System.out.println("-------\n\t Final map Element traversal for:");
    for(Map.Entry<Integer, String> entry:map.entrySet()){
        int k=entry.getKey();
        String v=entry.getValue();
        System.out.println(k+" = "+v);
    }
}

Run iterTest() in the main method, and the output is:

-------
	Element traversal of the final map:
2 = two
4 = four
6 = six
8 = eight
10 = ten

If will

iter.remove(); 

replace with

map.put(key, "Odd number");
//Or map.remove(key);

ConcurrentModificationException will be reported

Topics: JDK