Here you can find the source of inverse(Map
Parameter | Description |
---|---|
K | input type |
V | return type |
map | a bijection |
public static <K, V> Map<V, K> inverse(Map<K, V> map)
//package com.java2s; //License from project: Open Source License import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class Main { /**/*from w w w . j a v a2s. com*/ * @param <K> input type * @param <V> return type * @param map a bijection * @return the inverse of map */ public static <K, V> Map<V, K> inverse(Map<K, V> map) { Map<V, K> inverse = new HashMap<>(map.size()); for (Entry<K, V> entry : map.entrySet()) { if (inverse.put(entry.getValue(), entry.getKey()) != null) { throw new IllegalArgumentException(map + " is not a bijection."); } } return inverse; } }