Top community members
All Wiki Articles Create Wiki Article

Many years me and my friends we were volunteers on my university doing programming classes for young people.

I got main conclusion: Young people are awesome, especially students, because they always bring new and fresh ideas :)
* I loved to do it but my time is limited

  I decided to create something that will help millions of students

Now I am here

co-founder

Java - sort HashMap by values

0 contributions
14 points

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

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more