How to sort hash map?

Sort the map in ascending and decreasing order based on the key and value.

Approach: Using java 8- ascending sorting on a key basis

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

public class MapSort {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("B", 2);
        map.put("D", 4);
        map.put("G", 7);
        map.put("A", 1);

    // sort the map using key
        HashMap<String, Integer> mp = map.entrySet().stream().
                sorted(Map.Entry.comparingByKey()).collect(Collectors
                .toMap(Map.Entry::getKey, Map.Entry::getValue,
     (o1V, o2V) -> o1V, LinkedHashMap::new));

        // print hashmap
        mp.forEach((K, V) -> System.out.println(K + " " + V));
    }
}

Approach:  Using java 8- ascending sorting on a key basis

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

public class MapSort {
    public static void main(String[] args) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(2, 2);
        map.put(4, 4);
        map.put(7, 7);
        map.put(1, 1);

        // sort the map using key
        HashMap<Integer, Integer> mp = map.entrySet().stream().
                    sorted(Map.Entry.comparingByKey()).collect(
                Collectors.toMap(Map.Entry::getKey, 
            Map.Entry::getValue, (o1V, o2V) -> o1V, LinkedHashMap::new));

        // print hashmap
        mp.forEach((K, V) -> System.out.println(K + " " + V));
    }

}

Approach:  Using java 8- descending sorting on a key basis

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

public class MapSort {
    
    public static void main(String[] args) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(2, 2);
        map.put(4, 4);
        map.put(7, 7);
        map.put(1, 1);

        // sort the map using key
        HashMap<Integer, Integer> mp = map.entrySet().stream().
            sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())).collect(
                Collectors.toMap(Map.Entry::getKey, 
        Map.Entry::getValue, (o1V, o2V) -> o1V, LinkedHashMap::new));

        // print hashmap
        mp.forEach((K, V) -> System.out.println(K + " " + V));
    }
}


Approach:  Using java 8- ascending sorting on a value basis


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

public class MapSort {
    
    public static void main(String[] args) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(2, 4);
        map.put(4, 2);
        map.put(7, 8);
        map.put(1, 3);

        // sort the map using value
        HashMap<Integer, Integer> mp = map.entrySet().stream().
            sorted(Map.Entry.comparingByValue()).collect(
                Collectors.toMap(Map.Entry::getKey, 
        Map.Entry::getValue, (o1V, o2V) -> o1V, LinkedHashMap::new));

        // print hashmap
        mp.forEach((K, V) -> System.out.println(K + " " + V));
    }
}

Approach:  Using java 8- descending sorting on a value basis


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

public class MapSort {
    
    public static void main(String[] args) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(2, 4);
        map.put(4, 2);
        map.put(7, 8);
        map.put(1, 3);

        // sort the map using value
        HashMap<Integer, Integer> mp = map.entrySet().stream().
            sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).collect(
                Collectors.toMap(Map.Entry::getKey, 
        Map.Entry::getValue, (o1V, o2V) -> o1V, LinkedHashMap::new));

        // print hashmap
        mp.forEach((K, V) -> System.out.println(K + " " + V));
    }
}


No comments:

Post a Comment