# Sort a Map by Values in Java Using Stream or List

> Sort a Java Map by its values using a List with Collections.sort and comparingByValue, or the Stream API with a LinkedHashMap collector for ordered results

**URL:** https://sentry.io/answers/how-to-sort-a-map-key-value-by-values-in-java/

---

## The Problem

I don't know how to sort a `Map<Key, Value>` by its values in Java.

## The Solution

There are two ways to sort a map by its values in Java:

- You can use `List<Map.Entry<K, V>>` to create a list of its entries, then use `Collections.sort()` to sort that list based on the values of each entry.
- If you use Java version 8 or later, you can use the `Stream` API to organize the map entries by value in a `LinkedHashMap`.

Consider the following map as an example:

```java
Map<String, Integer> map = new HashMap<>();

map.put("Apple", 3);
map.put("Banana", 1);
map.put("Kiwi", 4);
map.put("Cherry", 2);
```

The `Banana` entry has the lowest value and would appear first in a sorted list.

### Using `List<Map.Entry<K, V>>` and `Collections.sort()`

You can convert the map's entry set into a list and use `Collections.sort()` to sort it based on the values.

```java
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("Apple", 3);
        map.put("Banana", 1);
        map.put("Kiwi", 4);
        map.put("Cherry", 2);

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

        for (Map.Entry<String, Integer> entry : list) {
            System.out.println(entry.getKey() + " -> " + entry.getValue());
        }
    }
}
```

This creates a list from the map entries, uses `sort()` with `Map.Entry.comparingByValue()` to sort the list by values, and returns the following output:

```
Banana -> 1
Cherry -> 2
Apple -> 3
Kiwi -> 4
```

### Using the `Stream` API (Java 8+)

If you are using Java version 8 or later, you can use the `Stream` API to sort the map by its values and collect the results into a `LinkedHashMap` to preserve the order of the sorted entries.

```java
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("Apple", 3);
        map.put("Banana", 1);
        map.put("Kiwi", 4);
        map.put("Cherry", 2);

        Map<String, Integer> sortedMap = map.entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue())
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (e1, e2) -> e1,  // Merge function
                        LinkedHashMap::new));

        sortedMap.forEach((key, value) -> System.out.println(key + " -> " + value));
    }
}
```

Running this outputs:

```
Banana -> 1
Cherry -> 2
Apple -> 3
Kiwi -> 4
```

---

*Source: [sentry.io/answers/how-to-sort-a-map-key-value-by-values-in-java/](https://sentry.io/answers/how-to-sort-a-map-key-value-by-values-in-java/)*
