reverse Map between key and value - Java java.util

Java examples for java.util:Map Value

Description

reverse Map between key and value

Demo Code


//package com.java2s;

import java.util.HashMap;

import java.util.Map;
import java.util.Map.Entry;

public class Main {

    public static <K, V> Map<V, K> reverseMap(Map<K, V> map) {
        if (map != null) {
            Map<V, K> retMap = new HashMap<V, K>();
            for (Entry<K, V> entry : map.entrySet()) {
                retMap.put(entry.getValue(), entry.getKey());
            }//w  w w.j  ava  2  s .  c o m
            return retMap;
        }
        return null;
    }
}

Related Tutorials