Java Map Invert inverse(Map map)

Here you can find the source of inverse(Map map)

Description

inverse

License

Open Source License

Parameter

Parameter Description
K input type
V return type
map a bijection

Return

the inverse of map

Declaration

public static <K, V> Map<V, K> inverse(Map<K, V> map) 

Method Source Code

//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;
    }
}

Related

  1. inverse(Map m, Map newMap)
  2. inverse(Map map)
  3. inverseGet(Map map, Object value)
  4. invert(Map map)
  5. invert(Map in)
  6. invert(Map map)