Maps { public static <K, V> Map<V, K> swapped(Map<K, V> map, Supplier<Map<V, K>> mapSupplier) { return map.entrySet().stream() .collect(Collectors.toMap( Map.Entry::getValue, Map.Entry::getKey, (key, value) -> { throw new IllegalStateException(String.format("Duplicate key %s", key)); }, mapSupplier )); }  static void update(Map<K, V> map, K key, Supplier<V> defaultValue, Function<V, V> updateFunction); static V put(Map<? super K, ? super V> map, K key, V value); static Map<V, K> swapped(Map<K, V> map, Supplier<Map<V, K>> mapSupplier); static Map.Entry<K, V> entryOf(K key, V value); static Map.Entry<K, V> immutableEntryOf(K key, V value); static Map<K, V> of(Object... values); @SuppressWarnings({ "unchecked", "unused" }) static Map<K, V> of(Class<K> keyType, Class<V> valueType, Object... values);  }
@Test void swapped() { Map<String, Integer> map = new HashMap<String, Integer>() {{ put("a", 1); put("b", 2); }}; Map<Integer, String> swapped = Maps.swapped(map, HashMap::new); Assertions.assertEquals("a", swapped.get(1)); Assertions.assertEquals("b", swapped.get(2)); }