0 votes
in JAVA by
Which of the following is a valid syntax to synchronize the HashMap?

a) Map m = hashMap.synchronizeMap();

b) HashMap map =hashMap.synchronizeMap();

c) Map m1 = Collections.synchronizedMap(hashMap);

d) Map m2 = Collection.synchronizeMap(hashMap);

1 Answer

0 votes
by

(c) Map m1 = Collections.synchronizedMap(hashMap);

Reason: By default, the HashMap class is a non-synchronized collection class. The need for synchronization is to perform thread-safe operations on the class. To synchronize the HashMap class explicitly, we should use the Collections.synchronizedMap(hashMap) method that returns a thread-safe map object.

...