简体   繁体   中英

Why changing the value of key changes the output in concurrent Hashmap?

I was working with concurrent HashMap on eclipse IDE and came across the fact that while changing the keys of a concurrent HashMap, my output also changes.
Case I:

 public static void main(String[] args) {       
    ConcurrentHashMap<String, String> concurrentHashMap= new ConcurrentHashMap<String, String>();
    concurrentHashMap.put("Fav Rap", "Eminem");
    concurrentHashMap.put("Fav Food", "Pizza");
    concurrentHashMap.put("Pop", "Jackson");

    for(Map.Entry<String, String> entry : concurrentHashMap.entrySet()){
        concurrentHashMap.put("Fav Game", "Fifa");
        concurrentHashMap.put("student", "smith");

        System.out.println("Key : "+entry.getKey()+",   Value : "+entry.getValue()+" hashcode: "+entry.hashCode()+" size: "+concurrentHashMap.size());
    }

}

The output is:

Key : Pop, Value : Jackson hashcode: -172386558 size: 5

Key : Fav Rap, Value : Eminem hashcode: 1491542025 size: 5

Key : student, Value : smith hashcode: -1988544968 size: 5

Key : Fav Game, Value : Fifa hashcode: 1043213001 size: 5

Key : Fav Food, Value : Pizza hashcode: 983035627 size: 5

Case II:

    public static void main(String[] args) {
    ConcurrentHashMap<String, String> concurrentHashMap= new ConcurrentHashMap<String, String>();
    concurrentHashMap.put("Fav1 Rap", "Eminem");
    concurrentHashMap.put("Fav Food", "Pizza");
    concurrentHashMap.put("Pop", "Jackson");

    for(Map.Entry<String, String> entry : concurrentHashMap.entrySet()){
        concurrentHashMap.put("Fav Game", "Fifa");
        concurrentHashMap.put("student", "smith");
        System.out.println("Key : "+entry.getKey()+",   Value : "+entry.getValue()+" hashcode: "+entry.hashCode()+" size: "+concurrentHashMap.size());
    }

}

The output is :

Key : Pop, Value : Jackson hashcode: -172386558 size: 5

Key : Fav1 Rap, Value : Eminem hashcode: 1157829666 size: 5

Key : Fav Food, Value : Pizza hashcode: 983035627 size: 5

I just changed the first key of the map from Fav to Fav1 and the output changes. Can you please clarify my doubt? Thanks in advance :)

This is the way the concurrent hash-map work. If you take a look after adding all the value the output would be same. As while iterating you are adding item in it that case behavior varies as per the object references in the iterator.

ConcurrentHashMap中的检索操作(包括get)不会被阻塞,因此可能与更新操作(包括put( 如您的情况 )和remove) 重叠

I think you should just try to recompile your project. Because I tried your code in my IDE. It worked well. It gave me 5 outputs just like on case I. Here my output when I changed Fav Rap to Fav1 Rap.

   Key : Fav Food,   Value : Pizza hashcode: 983035627 size: 5
   Key : Pop,   Value : Jackson hashcode: -172386558 size: 5
   Key : Fav1 Rap,   Value : Eminem hashcode: 1157829666 size: 5
   Key : Fav Game,   Value : Fifa hashcode: 1043213001 size: 5
   Key : student,   Value : smith hashcode: -1988544968 size: 5

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