Java Map Invert invertedMapFrom(Map source)

Here you can find the source of invertedMapFrom(Map source)

Description

Returns a map based on the source but with the key & values swapped.

License

BSD License

Parameter

Parameter Description
source Map

Return

Map

Declaration

public static <K, V> Map<V, K> invertedMapFrom(Map<K, V> source) 

Method Source Code

//package com.java2s;
/**//from   w  ww .  j  a  v  a 2 s .  c o m
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 */

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * Returns a map based on the source but with the key & values swapped.
     *
     * @param source
     *            Map
     * @return Map
     */
    public static <K, V> Map<V, K> invertedMapFrom(Map<K, V> source) {
        Map<V, K> map = new HashMap<>(source.size());
        for (Map.Entry<K, V> entry : source.entrySet()) {
            map.put(entry.getValue(), entry.getKey());
        }
        return map;
    }
}

Related

  1. invert(Map map)
  2. invert(Map in)
  3. invert(Map map)
  4. invert(Map map, String prefix)
  5. invert(Map map)
  6. invertMap(final Map map)
  7. invertMap(final Map map)
  8. invertMap(Map map)
  9. invertMap(Map map)