Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1.     protected static void incrementValue(Map<Long, Integer> counters, Long toAdd) {
  2.         Integer currValue = counters.get(toAdd);
  3.         if (currValue == null)
  4.             counters.put(toAdd, 1);
  5.         else
  6.             counters.put(toAdd, currValue+1);
  7.     }
  8.    
  9.     static List sortByValue(Map map) {
  10.         List list = new LinkedList(map.entrySet());
  11.         Collections.sort(list, new Comparator() {
  12.              public int compare(Object o1, Object o2) {
  13.                   return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
  14.              }
  15.         });
  16.  
  17.        Map result = new LinkedHashMap();
  18.        for (Iterator it = list.iterator(); it.hasNext();) {
  19.            Map.Entry entry = (Map.Entry)it.next();
  20.            result.put(entry.getKey(), entry.getValue());
  21.        }
  22.        Collections.reverse(list);
  23.        return list;
  24.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement