How to convert Map to List in Java

This article will introduce how to convert a Map to a List.

1. Using the List constructor

If a Collection object of the Key and Value of the Map is passed to the constructor of ArrayList, a list with all the elements of the Set is created.

The keySet() and values() functions return a Collection object that contains all the keys and values respectively.

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

public class Example {
    public static void main(String[] args) {

        Map<String, String> map = new HashMap<>();
        map.put("101", "apple");
        map.put("102", "kiwi");
        map.put("103", "orange");
        map.put("104", "banana");
        map.put("105", "peach");

        List<String> keyList = new ArrayList<>(map.keySet());
        List<String> valueList = new ArrayList<>(map.values());

        System.out.println(keyList);
        System.out.println(valueList);
    }
}

Output:

[101, 102, 103, 104, 105]
[apple, kiwi, orange, banana, peach]

Note that Map.keySet() and Map.values() return a Collection object containing the key or value elements.

And, since the Set class inherits from the Collection class, Set is also a Collection. below shows the return types of both methods.

// Map.java
Set<K> keySet();
Collection<V> values();

2. Using Stream

You can convert a Map to a List using Stream.

You can create a Stream for the key and value and convert it to a List, as follows.

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

public class Example {
    public static void main(String[] args) {

        Map<String, String> map = new HashMap<>();
        map.put("101", "apple");
        map.put("102", "kiwi");
        map.put("103", "orange");
        map.put("104", "banana");
        map.put("105", "peach");

        List<String> keyList = map.keySet().stream()
                .collect(Collectors.toCollection(ArrayList::new));
        List<String> valueList = map.values().stream()
                .collect(Collectors.toCollection(ArrayList::new));

        System.out.println(keyList);
        System.out.println(valueList);
    }
}

Output:

[101, 102, 103, 104, 105]
[apple, kiwi, orange, banana, peach]

2.1 Filtering

If you want to filter elements by some condition, it may be a good idea to use Stream to transform Map into a List.

For example, you can convert the rest of the elements except apple from Value into a list.

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

public class Example {
    public static void main(String[] args) {

        Map<String, String> map = new HashMap<>();
        map.put("101", "apple");
        map.put("102", "kiwi");
        map.put("103", "orange");
        map.put("104", "banana");
        map.put("105", "peach");

        List<String> valueList = map.values().stream()
                .filter((fruit)-> !fruit.equals("apple"))
                .collect(Collectors.toCollection(ArrayList::new));

        System.out.println(valueList);
    }
}

Output:

[kiwi, orange, banana, peach]
codechachaCopyright ©2019 codechacha