Skip to content

Instantly share code, notes, and snippets.

@arturotena
Created October 9, 2014 22:41
Show Gist options
  • Save arturotena/33a6b1cb579dc3262f42 to your computer and use it in GitHub Desktop.
Save arturotena/33a6b1cb579dc3262f42 to your computer and use it in GitHub Desktop.
Remove all entries except the given keys
/** 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