Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

hashmap sort by value descending

//Java code to sort a hashmap by VALUES in DESCENDING ORDER
Map<String, Integer> budget = new HashMap<>();
//initialize map with random values
Map<String, Integer> sorted = budget
        .entrySet()
        .stream()
        .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
        .collect(
            toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2,
                LinkedHashMap::new));
System.out.println("map after sorting by values in descending order: "
        + sorted);
 
PREVIOUS NEXT
Tagged: #hashmap #sort #descending
ADD COMMENT
Topic
Name
2+3 =