ConcurrentHashMap forEach and reduce reads in parallel

The bulk operations take a parallelism threshold and can traverse concurrently, which suits large read-mostly maps.

Code
var map = new ConcurrentHashMap<String, Integer>();
for (int i = 1; i <= 100; i++) map.put("k" + i, i);

long sum = map.reduceValues(1, Integer::longValue, Long::sum);
System.out.println("sum of 1..100 = " + sum);

Integer found = map.search(1, (k, v) -> v == 42 ? v : null);
System.out.println("search found = " + found);
System.out.println("mappingCount = " + map.mappingCount());
Output
sum of 1..100 = 5050
search found = 42
mappingCount = 100
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-30