I have been using Java\'s ConcurrentMap for a map that can be used from multiple threads. The putIfAbsent is a great method and is much easier to read/write than using stand
By keeping a pre-initialized value for each thread you can improve on the accepted answer:
Set initial = new HashSet();
...
Set set = map.putIfAbsent(name, initial);
if (set == null) {
set = initial;
initial = new HashSet();
}
set.add(Y);
I recently used this with AtomicInteger map values rather than Set.