Here you can find the source of reverse(Map
Parameter | Description |
---|---|
map | the map |
public static <K extends Comparable<K>, V> Map<K, V> reverse(Map<K, V> map)
//package com.java2s; //License from project: Apache License import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; public class Main { /**/*from w w w . j a v a2 s .c o m*/ * Reverse. * * @param map * the map * * @return the map< k, v> */ public static <K extends Comparable<K>, V> Map<K, V> reverse(Map<K, V> map) { List<Map.Entry<K, V>> mapEntry = new LinkedList<Map.Entry<K, V>>(map.entrySet()); Collections.reverse(mapEntry); LinkedHashMap<K, V> reversedMap = new LinkedHashMap<K, V>(); for (Iterator<Map.Entry<K, V>> it = mapEntry.iterator(); it.hasNext();) { Map.Entry<K, V> entry = it.next(); reversedMap.put(entry.getKey(), entry.getValue()); } return (reversedMap); } }