Post

Java ConcurrentHashSet

Java ConcurrentHashSet

Java gives you ConcurrentHashMap but no ConcurrentHashSet — which is mildly infuriating when you need a thread-safe set. Before Java 8, the workaround was wrapping a ConcurrentHashMap. Java 8 fixed this properly. Here are both approaches.

  • Java 8
1
ConcurrentHashMap.newKeySet()
  • Java 8
1
2
Map<String, Boolean> map=new ConcurrentHashMap();
Collections.newSetFromMap(map)

ConcurrentHashSet

This post is licensed under CC BY 4.0 by the author.