Here you can find the source of reverse( Map
public static <X extends Comparable<X>, Y> Map<Y, List<X>> reverse( Map<X, Y> map)
//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; } }