Returns a map based on the source but with the key & values swapped. - Java java.util

Java examples for java.util:Map Value

Description

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

Demo Code


//package com.java2s;
import java.util.HashMap;

import java.util.Map;

public class Main {
    /**/*w  w w .  ja  va 2  s . co m*/
     * 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<V, K>(source.size());
        for (Map.Entry<K, V> entry : source.entrySet()) {
            map.put(entry.getValue(), entry.getKey());
        }
        return map;
    }
}

Related Tutorials