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 :: check if field exists in java 
Java :: work with arrays java 
Java :: java how to create an array with existing objects 
Java :: string.indexof java 
Java :: javafx get the controller of a pane 
Java :: convert arraylist to array int 
Java :: fibonacci series in java 
Java :: from file to array java 
Java :: vscode code formatter cannot format 
Java :: android studio copy file 
Java :: line break in android xml dynamically 
Java :: add to list java 
Java :: java sort a map by keys 
Java :: local date to date java 
Java :: list of arrays java 
Java :: java comparing-two-csv-files-in-java 
Java :: codepointat java 
Java :: launch java batch 
Java :: java create arraylist with size 
Java :: Java How to use Map? 
Java :: kruskal java 
Java :: StackAsMyArrayList 
Java :: fragment view in XML android 
Java :: an internal error occurred during initializing java tooling . java.lang.nullpointerexception 
Java :: ArrayList of prime numbers 
Java :: Java Creating a Java LinkedList 
Java :: java class in dataweave 
Java :: fullscreen libgdx 
Java :: Implementation of TreeMap Class in Java map 
Java :: how to initialize main in java 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =