Code example for ConcurrentMap

Methods: get, putIfAbsent

0
public class InstanceTracker { 
 
    private static ConcurrentMap<String, Set<Object>> data = new ConcurrentHashMap<String, Set<Object>>();
 
    public static void add(String type, Object object) {
        Set<Object> set = data.get(type);
        if(set == null) {
            set = Collections.newSetFromMap(new MapMaker().weakKeys().<Object, Boolean>makeMap());
            Set<Object> existing = data.putIfAbsent(type, set);
            if(existing != null) {
                set = existing;
            } 
        } 
        set.add(object);
    } 
 
    public static Set<?> get(String type) {
        final Set<Object> result =  data.get(type);
        if(result != null) {
            return result;
        }