Java Map Reverse reverseMap(Map map)

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

Description

Returns a map where the keys are the values of the map argument and the values are the corresponding keys.

License

Open Source License

Parameter

Parameter Description
map a parameter

Declaration

public static <K, V> Map<V, K> reverseMap(Map<K, V> map) 

Method Source Code

//package com.java2s;
/**//w  ww  . ja va  2s. c  o m
 * Copyright (c) 2012 Todoroo Inc
 *
 * See the file "LICENSE" for the full license governing this code.
 */

import java.util.HashMap;

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

public class Main {
    /**
     * Returns a map where the keys are the values of the map argument
     * and the values are the corresponding keys. Use at your own
     * risk if your map is not 1-to-1!
     * @param map
     * @return
     */
    public static <K, V> Map<V, K> reverseMap(Map<K, V> map) {
        HashMap<V, K> reversed = new HashMap<V, K>();

        Set<Entry<K, V>> entries = map.entrySet();
        for (Entry<K, V> entry : entries) {
            reversed.put(entry.getValue(), entry.getKey());
        }
        return reversed;
    }
}

Related

  1. reverse_map(Map map)
  2. reverse_map(Map map)
  3. reverseKeyAndValue(Map map)
  4. reverseLabelMap(Map labelMap)
  5. reverseMap(List listSeq)
  6. reverseMap(Map map)
  7. reverseMap(Map map)
  8. reverseMap(Map map)
  9. reverseMap(Map map)