Java Map Reverse reverse_map(Map map)

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

Description

"{:a 1 :b 1 :c 2} -> {1 [:a :b] 2 :c}"

License

Open Source License

Parameter

Parameter Description
map a parameter

Declaration

public static <K, V> HashMap<V, List<K>> reverse_map(Map<K, V> map) 

Method Source Code

//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;
        }
    }
}

Related

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