Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Inverts the specified map: the values of the map become keys in the resulting map,
- * and the keys become values.
- *
- * Behavior of duplicate values (key overlap) in the new map is undefined: it depends
- * on the {@link Map.Entry} iterator of the supplied map.
- *
- * @param the map to be inverted. can be null
- * @return a new map, never null, but possibly empty (and immutable).
- */
- static <K,V> Map<V, K> invertMap(Map<K,V> map) {
- if (map == null) {
- return Collections.emptyMap();
- }
- return map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement