Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. /**
  2. * Inverts the specified map: the values of the map become keys in the resulting map,
  3. * and the keys become values.
  4. *
  5. * Behavior of duplicate values (key overlap) in the new map is undefined: it depends
  6. * on the {@link Map.Entry} iterator of the supplied map.
  7. *
  8. * @param the map to be inverted. can be null
  9. * @return a new map, never null, but possibly empty (and immutable).
  10. */
  11. static <K,V> Map<V, K> invertMap(Map<K,V> map) {
  12. if (map == null) {
  13. return Collections.emptyMap();
  14. }
  15. return map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement