Here you can find the source of invertMap(Map map)
Parameter | Description |
---|---|
map | the map to invert, may not be null |
Parameter | Description |
---|---|
NullPointerException | if the map is null |
@SuppressWarnings("unchecked") public static Map invertMap(Map map)
//package com.java2s; import java.util.*; public class Main { /**//from w w w . j av a2 s. c o m * Inverts the supplied map returning a new HashMap such that the keys of * the input are swapped with the values. * <p> * This operation assumes that the inverse mapping is well defined. * If the input map had multiple entries with the same value mapped to * different keys, the returned map will map one of those keys to the * value, but the exact key which will be mapped is undefined. * * @param map the map to invert, may not be null * @return a new HashMap containing the inverted data * @throws NullPointerException if the map is null */ @SuppressWarnings("unchecked") public static Map invertMap(Map map) { Map out = new HashMap(map.size()); for (Iterator it = map.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); out.put(entry.getValue(), entry.getKey()); } return out; } }