Here you can find the source of invert(Map
Parameter | Description |
---|---|
map | a parameter |
public static <K, V> Map<V, K> invert(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 { /**/* 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; } }