How to Convert a Map to List in Java

In this post, we will learn as how to Convert a map to List in Java.We will see how to do this using JDK. We will check how to do this using Java 8 and in case you are still using Java 1.7 or lower versions.

 

1. Convert a map to List in Java

To convert, Java Map to List, we can use the conventional constructor with ArrayList, however, there are few things to consider before we proceed with the conversion. Java Map has 2 values while list contains only single value while converting Map to list.While converting the map to list in Java, we can either convert the map keys or values to the List.There is no direct option to map both keys and values in List, but this can be done using a wrapper object. Let’s see how to do this using Java 1.7 or earlier versions.

public class MapToList {

    public static void main(String[] args) {

        Map<Integer,String> map= getMap();
        //Convert Map keys to List
        List<Integer> keyList= new ArrayList<Integer>(map.keySet());
        keyList.forEach(key-> System.out.println(key));

        //Convert Map values to List
        List<String> valueList= new ArrayList<String>(map.values());
        keyList.forEach(value-> System.out.println(value));

    }

    static Map<Integer,String> getMap(){

        Map<Integer, String> sampleMap=new HashMap<Integer,String>();
        sampleMap.put(1,"Sunday");
        sampleMap.put(2,"Monday");
        sampleMap.put(3,"Tuesday");
        sampleMap.put(4,"Wednesday");
        return sampleMap;
    }
}

Output for the above code is::

1
2
3
4

For valueList

Sunday
Monday
Tuesday
Wednesday

While working on these options, you should also remember these important points.

  1. map.values() method returns a Collection of the values.
  2. map.keySet() returns a Set containing all the map’s key.

 

2. Convert a map to List in Java8

Java 8 stream API provides few options to convert a map to List. We can use the combination of stream API and Collectors to accomplish this. One of the benefit of using Java 8 syntax is the readability of the code along with some of the build in performance boost. Keep in mind that the same restriction applies when using the Stream API and we may need a wrapper object if want to collect both keys and values from the Map.

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

        Map<Integer,String> map= getMap();
        List<Integer> keyList=map.keySet().stream().collect(Collectors.toList());
        keyList.forEach(key-> System.out.println(key));

        List<String> valueList=map.values().stream().collect(Collectors.toList());
        valueList.forEach(value-> System.out.println(value));
    }

    static Map<Integer,String> getMap(){

        Map<Integer, String> sampleMap=new HashMap<Integer,String>();
        sampleMap.put(1,"Sunday");
        sampleMap.put(2,"Monday");
        sampleMap.put(3,"Tuesday");
        sampleMap.put(4,"Wednesday");
        return sampleMap;
    }
}

The stream API also provide flexibility and methods to filter out the specific data while converting the map to list.Let’s say,we are only interested to get the data for the even keys, in traditional conversion, we need to first iterate and than compare the key, while with the Stream API, we can use the filter method on stream. This is really helpful in case we have larger map data.

public class MapToListExample {

    public static void main(String[] args) {
        Map < Integer, String > map = new HashMap < Integer, String > ();
        map.put(1, "one");
        map.put(2, "two");
        map.put(3, "three");
        map.put(4, "four");
        map.put(5, "five");
        map.put(6, "six");
        map.put(7, "seven");
        map.put(8, "eight");
        map.put(9, "nine");

        List < Integer > output = map.keySet()
            .stream()
            .filter(key - > key % 2 == 0)
            .collect(Collectors.toList());
        output.forEach(System.out::println);
    }
}

 

Summary

In this post, we explored as to how to convert a Map to List in Java, we learned how to convert it using a constructor and Java 8 Stream API.We can use the same approach to convert a map to set in Java.It works on the same concept as Set can only be used to store either keys or value until we use a wrapper object to store both keys and values. For more information, read how to convert map to set in Java .All the code of this article is available Over on Github. This is a Maven-based project.

Subscribe
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments