Here you can find the source of reverse_map(Map
Parameter | Description |
---|---|
map | a parameter |
public static <K, V> HashMap<V, List<K>> reverse_map(Map<K, V> map)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; public class Main { /**// www . j a v a 2 s .co m * "{:a 1 :b 1 :c 2} -> {1 [:a :b] 2 :c}" * * @param map * @return */ public static <K, V> HashMap<V, List<K>> reverse_map(Map<K, V> map) { HashMap<V, List<K>> rtn = new HashMap<V, List<K>>(); if (map == null) { return rtn; } for (Entry<K, V> entry : map.entrySet()) { K key = entry.getKey(); V val = entry.getValue(); List<K> list = rtn.get(val); if (list == null) { list = new ArrayList<K>(); } list.add(key); rtn.put(entry.getValue(), list); } return rtn; } }