List of utility methods to do Map Reverse
Map | sortByValue(Map map, boolean reverse) sort By Value List list = new LinkedList(map.entrySet()); Collections.sort(list, new Comparator() { @Override public int compare(Object o1, Object o2) { return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue()); }); if (reverse) { ... |
Map | sortByValue(Map Sorts a map based on value, returns sorted map with predictable iteration Initial code from: http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet()); Comparator<Map.Entry<K, V>> comparator = new Comparator<Map.Entry<K, V>>() { public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { return (o1.getValue()).compareTo(o2.getValue()); }; if (doReverse) { Collections.sort(list, Collections.reverseOrder(comparator)); ... |
Map | toReverseIndex(Map to Reverse Index Map<String, Map<Integer, String>> reverseForwardIndex = new HashMap<String, Map<Integer, String>>(); for (String dimensionName : forwardIndex.keySet()) { Map<String, Integer> map = forwardIndex.get(dimensionName); reverseForwardIndex.put(dimensionName, new HashMap<Integer, String>()); for (Entry<String, Integer> entry : map.entrySet()) { reverseForwardIndex.get(dimensionName).put(entry.getValue(), entry.getKey()); return reverseForwardIndex; |