Multiple choice technology programming languages

HashMap can be synchronized by _______ ?

  1. Map m = Collection.synchronizeMap(hashMap);

  2. Map m = Collections.synchronizeMap(hashMap);

  3. Map m = hashMap.synchronizeMap();

  4. none

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

HashMap is not thread-safe, but can be wrapped using Collections.synchronizedMap() to obtain a synchronized wrapper. Option B correctly shows the syntax: Map m = Collections.synchronizeMap(hashMap); - note the class is Collections (plural), not Collection. Option A is incorrect because Collection.synchronizeMap doesn't exist.