Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

As a website resource for companies and engineering enthusiasts to comply with the newest and greatest breakthroughs in Unified Communications, IP Telephony, Hosted Communications and VoIP.
- internet
Odd behaviour, the result of the last modification comes out as HashMap after iterator: {3=new3, 2=1, 1=1, 6=1, 5=1, 4=1}
- Khalid
defnetly we get confused about Output. Can you Explain more reagarding the above Program…When we adding the element first time it provides an Exception…But When you have Written “Map.put(key, “new3”);” this line it doesnt provide any error.Explain it in better way…
- Ashok singh
Excellent! Clears the concept and the usage of ConcurrentHashMap Thanks
- Rajeev
I have added code to remove one item from map. the item is removed from map. But when its iterating it is printing the key and with value as null. Does that mean iterator has that key even though map does not have it? Does iterator will have a copy of keys? Below is modified code: while(it.hasNext()){ String key = it.next(); System.out.println("Key "+key+"value: "+myMap.get(key)); if(key.equals(“3”)) { myMap.put(key+“new”, “new3”); myMap.remove(“4”); System.out.println("while iterating: "+myMap); } } System.out.println("ConcurrentHashMap after iterator: "+myMap); ouput is: ConcurrentHashMap before iterator: {1=1, 5=1, 6=1, 3=1, 4=1, 2=1} Key 1 value: 1 Key 5 value: 1 Key 6 value: 1 Key 3 value: 1 while iterating: {1=1, 3new=new3, 5=1, 6=1, 3=1, 2=1} Key 4 value: null Key 2 value: 1 ConcurrentHashMap after iterator: {1=1, 3new=new3, 5=1, 6=1, 3=1, 2=1}
- Sravanthi
For the key “4” : while(it1.hasNext()){ String key = it1.next(); if(key.equals(“4”)){ myMap.put(key+“new”, “new4”); } } Output: Got no ConcurrentModificationException occured… HashMap before iterator: {3=1, 2=1, 1=1, 6=1, 5=1, 4=1} HashMap after iterator: {3=1, 2=1, 1=1, 4new=new4, 6=1, 5=1, 4=1} How???
- Nazeer Khan
Nicely explained. can you please tell why there is no exception when we are try to change the value but not key. //myMap.put(key+“new”, “new3”); myMap.put(key, “new3”); do we have seperate “modCount” for key and value in map. but how it is getting a updated value?
- Devendra
Nicely compiled article. Understood the concept quickly. Thank you.
- Ankit Tripathi
if u are breaking from while when key is 3 then how could it print the remaining values in while loop if(key.equals(“3”)){ myMap.put(key+“new”, “new3”); break; } ConcurrentHashMap before iterator: {1=1, 5=1, 6=1, 3=1, 4=1, 2=1} ConcurrentHashMap after iterator: {1=1, 3new=new3, 5=1, 6=1, 3=1, 4=1, 2=1} HashMap before iterator: {3=1, 2=1, 1=1, 6=1, 5=1, 4=1} HashMap after iterator: {3=1, 2=1, 1=1, 3new=new3, 6=1, 5=1, 4=1} The output should be ConcurrentHashMap before iterator: {1=1, 5=1, 6=1, 3=1, 4=1, 2=1} ConcurrentHashMap after iterator: {1=1, 3new=new3, 5=1, 6=1, 3=1, 4=1, 2=1} HashMap before iterator: {3=1, 2=1, 1=1, 6=1, 5=1, 4=1} HashMap after iterator: {3=1, 2=1, 1=1, 3new=new3} Please clarify
- nikhil