Created
January 16, 2018 12:14
-
-
Save adamw/78a3a73c3ac9a63a25f9f3191103f9b4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<K, V> void addToMultimap(ConcurrentHashMap<K, Set<V>> map, K key, V value) { | |
map.compute(key, (k, v) -> { | |
if (v == null) { | |
v = ConcurrentHashMap.newKeySet(); | |
} | |
v.add(value); | |
return v; | |
}); | |
} | |
<K, V> void removeFromMultimap(ConcurrentHashMap<K, Set<V>> map, K key, V value) { | |
map.computeIfPresent(key, (k, v) -> { | |
v.remove(value); | |
if (v.size() == 0) { | |
return null; | |
} else { | |
return v; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah, something like this: