hashmap sort ascending

https://‮ttual.www‬uri.com
hashmap sort ascending

To sort a HashMap in ascending order by its keys or values, you can use the TreeMap class, which is a Map implementation that maintains the keys in ascending order.

Here's an example of how you can sort a HashMap in ascending order by its keys in Java:

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        // create a hash map
        Map<String, Integer> map = new HashMap<>();

        // add key-value pairs to the map
        map.put("Apple", 1);
        map.put("Banana", 2);
        map.put("Orange", 3);

        // sort the map by keys using a tree map
        Map<String, Integer> sortedMap = new TreeMap<>(map);

        // iterate over the sorted map
        for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

This code creates a HashMap called map and adds three key-value pairs to it: "Apple" => 1, "Banana" => 2, and "Orange" => 3. It then creates a TreeMap called sortedMap and passes the map as an argument to the constructor. The TreeMap maintains the keys in ascending order, so the key-value pairs will be sorted in ascending order by the keys. Finally, it iterates over the sorted map and prints the key-value pairs.

To sort the HashMap in ascending order by its values, you can use a custom Comparator that compares the values of the entries and sorts them in ascending order. Here's an example of how you can do this:

import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        // create a hash
        HashMap<String, Integer> fruits = new HashMap<>();

		fruits.put("Apple", 21);
		fruits.put("Banana", 12);
		fruits.put("Cherry", 15);
		fruits.put("Orange", 35);
		fruits.put("Lautturi", 32);

		Map<String, Integer> sortedFruits = fruits.entrySet()
				.stream()
				
//              .sorted(Map.Entry.comparingByValue()) // sorting in natural/ascending order
//              .sorted(Map.Entry.comparingByValue((o1, o2) -> Integer.compare(o1, o2))) // sorting in natural/ascending order
//              .sorted(Map.Entry.comparingByValue((o1, o2) -> Integer.compare(o2, o1))) // sorting in reverse/descending order
//              .sorted((x, y) -> x.getValue()-y.getValue())  // sorting in natural/ascending order
//              .sorted((x, y) -> y.getValue()-x.getValue())   // sorting map/hashmap in reverse/descending order 
//              .sorted(Map.Entry.comparingByValue(Comparator.naturalOrder())) // sorting hashmap/map in natural/ascending order
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))  // sorting map in reverse/descending order

				.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
		System.out.println("original map:" + fruits);
		System.out.println("sorted map:" + sortedFruits);

	}
}
Created Time:2017-11-01 12:05:05  Author:lautturi