Java Map Sort sortByValue(Map map)

Here you can find the source of sortByValue(Map map)

Description

sort By Value

License

Open Source License

Declaration

public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
        List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
        Collections.sort(list, (o1, o2) -> -((o1.getValue()).compareTo(o2.getValue())));

        Map<K, V> result = new LinkedHashMap<>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }/*from w ww  .ja v  a2s .  c om*/
        return result;
    }
}

Related

  1. sortByValue(Map map)
  2. sortByValue(Map map)
  3. sortByValue(Map map, Comparator aComp)
  4. sortByValue(Map map)
  5. sortByValue(Map map)
  6. sortByValue(Map map)
  7. sortByValue(Map map)
  8. sortByValue(Map map)
  9. sortByValue(Map map)