Java Map Invert invert(Map map)

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

Description

Method to invert a map i.e keys becomes values and vice versa

License

Open Source License

Parameter

Parameter Description
map a parameter

Return

Map

Declaration

public static <K, V> Map<V, K> invert(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 {
    /**/*  w w w . j av a 2  s  . co  m*/
     * Method to invert a map i.e keys becomes values and vice versa
     * 
     * @param map
     * @return {@link Map<V, K>}
     */
    public static <K, V> Map<V, K> invert(Map<K, V> map) {
        if (map != null) {
            Map<V, K> returnValue = new HashMap<>();
            for (Entry<K, V> entry : map.entrySet()) {
                if (entry != null) {
                    returnValue.put(entry.getValue(), entry.getKey());
                }
            }
            return returnValue;
        }
        return null;
    }
}

Related

  1. inverse(Map map)
  2. inverse(Map map)
  3. inverseGet(Map map, Object value)
  4. invert(Map map)
  5. invert(Map in)
  6. invert(Map map, String prefix)
  7. invert(Map map)
  8. invertedMapFrom(Map source)
  9. invertMap(final Map map)