sort By Value in Map Ascending - Java Collection Framework

Java examples for Collection Framework:Map

Description

sort By Value in Map Ascending

Demo Code


    //package com.java2s;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.LinkedHashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;

    public class Main {
        public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueAscending(Map<K, V> map) {
   return sortByValue(map, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));
}

        private static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(
                Map<K, V> map, Comparator<Map.Entry<K, V>> comparator) {

            List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
            Collections.sort(list, comparator);

            Map<K, V> result = new LinkedHashMap<>();
            for (Map.Entry<K, V> entry : list) {
                result.put(entry.getKey(), entry.getValue());
            }//from  w  w w . j a  v  a  2  s .c  om

            return result;
        }
    }

Related Tutorials