Search
 
SCRIPT & CODE EXAMPLE
 

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);
Comment

hashmap sort ascending

LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
 
unSortedMap.entrySet()
    .stream()
    .sorted(Map.Entry.comparingByKey())
    .forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
Comment

PREVIOUS NEXT
Code Example
Java :: java random 5 digit int 
Java :: difference between java and javax 
Java :: get logged-in user in Spring Security 
Java :: change drawable color programmatically android 
Java :: java write 
Java :: java swing alert window 
Java :: java csv 
Java :: How to perform counting sort in Java? 
Java :: Get the first day of the current month in Java 
Java :: java how to make a gui 
Java :: calendar java add 1 day 
Java :: .now() java 
Java :: turning a sentence to an array java 
Java :: {} when initialising arraylist java 
Java :: loop will array length java example 
Java :: scanner check if int 
Java :: check if all values are same in list java 
Java :: how to declare array java 
Java :: iterating over a hashmap 
Java :: turn java code to .exe 
Java :: como calcular a raiz quadrada em java 
Java :: java foreach 
Java :: get username from jwt token request spring boot 
Java :: change button text onclick java 
Java :: java check two dates same day 
Java :: android studio, not loading url in webview 
Java :: get request java 
Java :: android dialog 
Java :: how to get the average from a list in java 
Java :: int to byte 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =