Thursday, August 4, 2016

Java 1.8: Sorting HashMap by value

Here is an simple example how easy sorting map is in Java 1.8
public class SortingMapExample {
   public Map getSortedMap(){
     Map<K,V> sortedMap = inputMap.entrySet().stream()
             .sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
             .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
     return sortedMap;
    }
}

No comments:

Post a Comment

AEM Page Manager API: Creating valid & Unique name programatically.

This post has been moved to below link AEM Solution: Creating AEM JCR valid & Unique name programatically https://followcybersecu...