Java Map Reverse reverse( Map map)

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

Description

reverse

License

Open Source License

Declaration

public static <X extends Comparable<X>, Y> Map<Y, List<X>> reverse(
            Map<X, Y> map) 

Method Source Code

//package com.java2s;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import java.util.List;
import java.util.Map;

public class Main {
    public static <X extends Comparable<X>, Y> Map<Y, List<X>> reverse(
            Map<X, Y> map) {
        Map<Y, List<X>> result = new HashMap<Y, List<X>>();

        for (X key : map.keySet()) {
            Y value = map.get(key);/*from ww w  .j a  v a  2s  .c o m*/

            if (!result.containsKey(value)) {
                result.put(value, new ArrayList<X>());
            }

            result.get(value).add(key);
        }

        for (Y key : result.keySet()) {
            Collections.sort(result.get(key));
        }

        return result;
    }
}

Related

  1. reverse(final Map map)
  2. reverse(Map source, Map target)
  3. reverse(Map source, Map target)
  4. reverse(Map in)