-1

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 :)

Sumeet Barua
  • 13
  • 1
  • 7
  • because fav rap and fav1 rap are different objects. That's why even though you just changed the name of the key it will still create a new object fav1 rap – Mr.Aw Mar 22 '17 at 07:15
  • I don't see any change in key for the current entry in your code. You are just basically adding 2 more entries in your map. – imprezzeb Mar 22 '17 at 07:17
  • @Mr.Aw I do agree it will create a new object of Fav1 rap. But what happens to the other two entries viz Fav Game and student. Although the size of list seems to be 5, but the no of data displayed is 3. Hope you understood my concern. – Sumeet Barua Mar 22 '17 at 07:33
  • @imprezzeb if u keenly see in case II I am changing the key from Fav Rap(case I) to Fav1 Rap(case II). ok. So now i do agree it creates a new object for Fav1 Rap. But while displaying why does it prints only 3 values although the size seems to be 5 in second case. Hope you understood my concern. – Sumeet Barua Mar 22 '17 at 07:38
  • @SumeetBarua I thought you have just showed 3 for us to see the changes in hashcode. I tried your code. I also tried to change the first key from Fav to Fav1. But the output doesn't change for me. It still gave me 5 outputs – Mr.Aw Mar 22 '17 at 08:14
  • 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 – Mr.Aw Mar 22 '17 at 08:15

3 Answers3

2

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.

Aditya
  • 41
  • 1
  • 4
  • if u keenly see in case II I am changing the key from Fav Rap(case I) to Fav1 Rap(case II). ok. So now i do agree it creates a new object for Fav1 Rap. But while displaying why does it prints only 3 values although the size of map seems to be 5 in second case. Hope you understood my concern. – Sumeet Barua Mar 22 '17 at 07:39
  • this is because you have iterating on entryset object of the map which was constructed with 3 values and while iterating the map content has added 2 extra values but entryset taken still pointing to the previous state not the current one. – Aditya Mar 22 '17 at 07:48
  • What about case I ? In case I also i am iterating on the same entryset object but it prints all the 5 values. Only when I change the key (String) from Fav Rap to Fav1 Rap, it terminates the loop after displaying 3 values but the size of the map still remains 5. Appreciate your help :) – Sumeet Barua Mar 22 '17 at 08:02
  • this will answer your question http://stackoverflow.com/a/38473153/2633386 – Aditya Mar 22 '17 at 10:31
0

Retrieval operations (including get) in ConcurrentHashMap do not block, so may overlap with update operations (including put(as in your case) and remove).

rcde0
  • 4,192
  • 3
  • 21
  • 31
  • Thanks for answering. if u keenly see in case II I am changing the key from Fav Rap(case I) to Fav1 Rap(case II). ok. So now i do agree it creates a new object for Fav1 Rap. But while displaying why does it prints only 3 values although the size seems to be 5 in second case. Hope you understood my concern. – Sumeet Barua Mar 22 '17 at 07:40
0

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
Mr.Aw
  • 216
  • 3
  • 16
  • oh is it !!! I was working on eclipse IDE though. Anyways thanks a lot. I have to figure it out whats wrong while working on eclipse. – Sumeet Barua Mar 22 '17 at 08:54
  • Did it help? Can you help me gain reputation by checking my answer and up vote? haha. – Mr.Aw Mar 22 '17 at 08:57