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