Created
October 9, 2014 22:41
-
-
Save arturotena/33a6b1cb579dc3262f42 to your computer and use it in GitHub Desktop.
Remove all entries except the given keys
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
/** Remove all entries except the given keys */ | |
private <K,V>void removeAllExcept(Map<K,V> map, K... keys) { | |
Map<K,V> objs = new HashMap<K,V>(keys.length); | |
for (K key : keys) | |
objs.put(key, map.get(key)); | |
map.clear(); | |
map.putAll(objs); | |
} | |
/** Remove all entries except the given keys */ | |
private <K,V>void removeAllExcept(Map<K,V> map, Collection<K> keys) { | |
Map<K,V> objs = new HashMap<K,V>(keys.size()); | |
for (K key : keys) | |
objs.put(key, map.get(key)); | |
map.clear(); | |
map.putAll(objs); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment