• Home
  • Recent Q&A
  • Java
  • Cloud
  • JavaScript
  • Python
  • SQL
  • PHP
  • HTML
  • C++
  • Data Science
  • DBMS
  • Devops
  • Hadoop
  • Machine Learning
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

(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.

Related questions

0 votes
asked Apr 8 in JAVA by SakshiSharma
0 votes
asked Apr 9 in JAVA by Robindeniel