Here you can find the source of reverseMap(Map
Parameter | Description |
---|---|
map | a parameter |
public static <K, V> Map<V, K> reverseMap(Map<K, V> map)
//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; } }