Code example for ConcurrentHashMap

0
 */ 
public class ObjectCounter<K> { 
    private final ConcurrentHashMap<K, AtomicInteger> map;
 
    public ObjectCounter() { 
        this.map = new ConcurrentHashMap<K, AtomicInteger>();
    } 
 
    /** 
     *  Add one. 
     *  @return count after increment 
     */ 
    public int increment(K h) {
        AtomicInteger i = this.map.putIfAbsent(h, new AtomicInteger(1));
        if (i != null)
            return i.incrementAndGet();
        return 1; 
    } 
 
    /** 
     *  @return current count