Example:

In this example we have a HashMap<Integer, String> it is having integer keys and String type values. In order to synchronize it we are using Collections.synchronizedMap(hashmap) it returns a thread-safe map backed up by the specified HashMap.

Important point to note in the below example:
Iterator should be used in a synchronized block even if we have synchronized the HashMap explicitly (As we did in the below code).

Comments

What’s the use of synchronizing hashmap?
As sync says only one at a time — which part of example says that only one is getting accessed?
Basically i wanted to know the difference b/w sync and non sync hashmap using multi threaded example and basically showing the difference