Here you can find the source of reverseMapping(Map
Parameter | Description |
---|---|
map | the map to reverse |
public static <K, V> Map<V, List<K>> reverseMapping(Map<K, V> map)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { /**/* w ww . jav a2 s . com*/ * reverse a Map to give a mapping from values to a list of keys * @param map the map to reverse * @return the reversed map */ public static <K, V> Map<V, List<K>> reverseMapping(Map<K, V> map) { Map<V, List<K>> rev = new HashMap<>(); for (Map.Entry<K, V> e : map.entrySet()) { V v = e.getValue(); if (!rev.containsKey(v)) { rev.put(v, new ArrayList<>()); } rev.get(v).add(e.getKey()); } return rev; } }