Java Map Invert invertMap(Map map)

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

Description

Inverts the supplied map returning a new HashMap such that the keys of the input are swapped with the values.

License

Open Source License

Parameter

Parameter Description
map the map to invert, may not be null

Exception

Parameter Description
NullPointerException if the map is null

Return

a new HashMap containing the inverted data

Declaration

@SuppressWarnings("unchecked")
public static Map invertMap(Map map) 

Method Source Code

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

Related

  1. invert(Map map)
  2. invertedMapFrom(Map source)
  3. invertMap(final Map map)
  4. invertMap(final Map map)
  5. invertMap(Map map)
  6. invertMap(Map map)
  7. invertMap(Map map)
  8. invertMap(Map map)
  9. invertStringMap(Map input)