← Quiz: Java Concurrency Utilities: all 10 questions
Quiz: Java Concurrency Utilities · Question 4 of 10
Which call counts a word in a ConcurrentHashMap safely from many threads?
Which call counts a word in a ConcurrentHashMap safely from many threads?
- A.map.put(w, map.get(w) + 1)
- B.if (map.containsKey(w)) map.put(w, map.get(w) + 1)
- C.map.merge(w, 1, Integer::sum)
- D.map.putAll(Map.of(w, 1))
Answer & explanation
Correct answer: map.merge(w, 1, Integer::sum)
merge performs the read-modify-write as one atomic step; the other options are separate reads and writes.
Can you get the rest right without peeking?
Take the timed Quiz: Java Concurrency Utilities quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →