-
-
Save dpash/bb5bd8a0720ba42c5f5dba68ab334317 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CounterConcurrentHashMap<K> { | |
private final Map<K, MutableInteger> counters = new ConcurrentHashMap<>(); | |
public int up(K key) { | |
MutableInteger initValue = new MutableInteger(1); | |
MutableInteger oldValue = counters.put(key, initValue); | |
if (oldValue != null) { | |
initValue.set(oldValue.get() + 1); | |
} | |
return initValue.get(); | |
} | |
public int getCount(K key) { | |
return counters.getOrDefault(key, new MutableInteger(0)).get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment