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: Open Source 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 { /**/*from w w w .j a v a 2 s .c o 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; } public static Object add(Object oldValue, Object newValue) { if (oldValue == null) { return newValue; } if (oldValue instanceof Long) { if (newValue == null) { return (Long) oldValue; } else { return (Long) oldValue + (Long) newValue; } } else if (oldValue instanceof Double) { if (newValue == null) { return (Double) oldValue; } else { return (Double) oldValue + (Double) newValue; } } else { return null; } } }