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

Apache 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: 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;
    }
}

Related

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