简体   繁体   中英

java.util.HashMap----look the console, why the print order is like this

Today I study deleting the special object from the java.util.HashMap. I test the wrong syntax. But I encountered a fantastic problem. The message on the console is not in the order. The exception message is printed by the wrong order. It seems the the program executes with multithreading. The following is my code.

package com.study.iter;

import java.util.HashMap;
import java.util.Map;

public class TestRmObjFromMap {

    public static void main(String[] args) {

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

        map.put("1", 1);
        map.put("2", 2);
        map.put("3", 3);
        map.put("4", 4);

        remove1(map);
    }

    private static void remove1(Map<String, Integer> map) {

        for(Map.Entry<String, Integer> entry : map.entrySet()) {

            if(entry.getKey().equals("3")) {
                map.remove(entry.getKey());
            }
            else {
                System.out.println("key: " + entry.getKey() + " -- value: " + entry.getValue());
            }

        }

    }

}

After I run this code, it print the following:

Exception in thread "main" java.util.ConcurrentModificationException
key: 1 -- value: 1
key: 2 -- value: 2
    at java.util.HashMap$HashIterator.nextNode(HashMap.java:1437)
    at java.util.HashMap$EntryIterator.next(HashMap.java:1471)
    at java.util.HashMap$EntryIterator.next(HashMap.java:1469)
    at com.study.iter.TestRmObjFromMap.remove1(TestRmObjFromMap.java:29)
    at com.study.iter.TestRmObjFromMap.main(TestRmObjFromMap.java:24)

Why the exception message is divided by other message. Why not like this:

key: 1 -- value: 1
key: 2 -- value: 2
Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.nextNode(HashMap.java:1437)
    at java.util.HashMap$EntryIterator.next(HashMap.java:1471)
    at java.util.HashMap$EntryIterator.next(HashMap.java:1469)
    at com.study.iter.TestRmObjFromMap.remove1(TestRmObjFromMap.java:29)
    at com.study.iter.TestRmObjFromMap.main(TestRmObjFromMap.java:24)

Who can tell me the reason? Thanks!

Now is a simple test:

public static void main(String[] args) {

    for(int i=0;i<500;i++) {
        System.out.println("ttt");
        if(i==10) throw new ConcurrentModificationException();
    }

}

the result:

Exception in thread "main" java.util.ConcurrentModificationException
    ttt
    at com.study.iter.TestIter.main(TestIter.java:14)
    ttt
    ttt
    ttt
    ttt
    ttt
    ttt
    ttt
    ttt
    ttt
    ttt

The reason:

 Exceptions will printed at stderr and System.out.println prints on stdout. Both streams are not sychronized

Thanks!

Exceptions will printed at stderr and System.out.println prints on stdout. Both streams are not sychronized

In a HashMap the iteration order is not guaranteed to be the insertion order, ie the data can be returned in any order its likes.

To have iteration order the same as insertion order use a LinkedHashMap instead

eg

Map<String, Integer> map = new LinkedHashMap();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM