Skip to content

Instantly share code, notes, and snippets.

@jaydenlin
Created December 21, 2013 02:32
Show Gist options
  • Save jaydenlin/8064688 to your computer and use it in GitHub Desktop.
Save jaydenlin/8064688 to your computer and use it in GitHub Desktop.
Set operation
public static <T, E> Set<T> getKeysByValue(Map<T, E> map, E value) {
Set<T> keys = new HashSet<T>();
for (Entry<T, E> entry : map.entrySet()) {
if (value.equals(entry.getValue())) {
keys.add(entry.getKey());
}
}
return keys;
}
public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
for (Entry<T, E> entry : map.entrySet()) {
if (value.equals(entry.getValue())) {
return entry.getKey();
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment