Advertisement
ivanov_ivan

Comparator

Apr 23rd, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.51 KB | None | 0 0
  1. public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
  2.     List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
  3.     Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
  4.         @Override
  5.         public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
  6.             return (e1.getValue()).compareTo(e2.getValue());
  7.         }
  8.     });
  9.  
  10.     Map<K, V> result = new LinkedHashMap<>();
  11.     for (Map.Entry<K, V> entry : list) {
  12.         result.put(entry.getKey(), entry.getValue());
  13.     }
  14.  
  15.     return result;
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement