Languages
[Edit]
EN

Java - sort HashMap by values

14 points
Created by:
Nikki-Mathews
517

Short solution:

Map<String, Integer> map = new HashMap<>();
// put key / value to map

List<Map.Entry<String, Integer>> sorted = new ArrayList<>(map.entrySet());
sorted.sort(Map.Entry.comparingByValue());

// we iterate over sorted entry list
for (Map.Entry<String, Integer> entry : sorted) {
    String key = entry.getKey();
    Integer value = entry.getValue();

    System.out.println("Key: " + key + ", Value : " + value);
}

Another short solution:

Map<String, Integer> map = new HashMap<>();
// put key / value to map

map.entrySet()
        .stream()
        .sorted(Map.Entry.comparingByValue())
        .forEach(entry -> {
            String key = entry.getKey();
            Integer value = entry.getValue();

            System.out.println("Key: " + key + ", Value : " + value);
        });

1. Sort HashMap by values using list and Map.Entry.comparingByValue() comparator

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Example1 {

    public static void main(String[] args) {

        Map<String, Integer> map = new HashMap<>();
        map.put("YY", 3);
        map.put("ZZ", 4);
        map.put("CC", 2);
        map.put("AA", 1);

        System.out.println("Unsorted:");
        map.forEach((key, value) -> {
            System.out.println("Key: " + key + ", Value : " + value);
        });

        System.out.println("---------");

        List<Map.Entry<String, Integer>> sorted = new ArrayList<>(map.entrySet());
        sorted.sort(Map.Entry.comparingByValue());

        System.out.println("Sorted:");
        // we iterate over sorted entry list
        for (Map.Entry<String, Integer> entry : sorted) {
            String key = entry.getKey();
            Integer value = entry.getValue();

            System.out.println("Key: " + key + ", Value : " + value);
        }
    }
}

Output:

Unsorted:
Key: YY, Value : 3
Key: ZZ, Value : 4
Key: CC, Value : 2
Key: AA, Value : 1
---------
Sorted:
Key: AA, Value : 1
Key: CC, Value : 2
Key: YY, Value : 3
Key: ZZ, Value : 4

2. Sort HashMap by values using stream and Entry comparator

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

public class Example2 {

    public static void main(String[] args) {

        Map<String, Integer> map = new HashMap<>();
        map.put("YY", 3);
        map.put("ZZ", 4);
        map.put("CC", 2);
        map.put("AA", 1);

        System.out.println("Unsorted:");
        map.forEach((key, value) -> {
            System.out.println("Key: " + key + ", Value : " + value);
        });

        System.out.println("---------");

        System.out.println("Sorted:");
        map.entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue())
                .forEach(entry -> {
                    String key = entry.getKey();
                    Integer value = entry.getValue();

                    System.out.println("Key: " + key + ", Value : " + value);
                });
    }
}

Output:

Unsorted:
Key: YY, Value : 3
Key: ZZ, Value : 4
Key: CC, Value : 2
Key: AA, Value : 1
---------
Sorted:
Key: AA, Value : 1
Key: CC, Value : 2
Key: YY, Value : 3
Key: ZZ, Value : 4

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java Collections - HashMap

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join