Convert a Map to an Array, List and Set in Java

In this article, we will show how to convert the values of a Map to an Array, a List or a Set using Java.
Learn and master Java Collections Framework at Learn Java Collections Framework.

1. Convert Map Values to Array

First, let’s look at converting the values of the Map into an array, using plain java:
package net.javaguides.hibernate;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class MapToArrayListSetExample {
    public String[] mapValuesToArray(Map < Integer, String > sourceMap) {
        Collection < String > values = sourceMap.values();
        String[] targetArray = values.toArray(new String[values.size()]);
        return targetArray;
    }

    public static void main(String[] args) {

        MapToArrayListSetExample arrayListSetExample = new MapToArrayListSetExample();
        Map < Integer, String > sourceMap = new HashMap < > ();
        sourceMap.put(100, "ABC");
        sourceMap.put(101, "PQR");
        sourceMap.put(102, "XYZ");
        String[] targetArray = arrayListSetExample.mapValuesToArray(sourceMap);
        System.out.println(Arrays.toString(targetArray));
    }
}
Output:
[ABC, PQR, XYZ]

2. Convert Map Values to List

let’s write a Java program to convert Map values to List:
package net.javaguides.hibernate;

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

public class MapValuesToListExample {

    public List < String > mapValuesToList(Map < Integer, String > sourceMap) {
        List < String > targetList = new ArrayList < > (sourceMap.values());
        return targetList;
    }

    public static void main(String[] args) {

        MapValuesToListExample arrayListSetExample = new MapValuesToListExample();
        Map < Integer, String > sourceMap = new HashMap < > ();
        sourceMap.put(100, "ABC");
        sourceMap.put(101, "PQR");
        sourceMap.put(102, "XYZ");
        List < String > targetList = arrayListSetExample.mapValuesToList(sourceMap);
        System.out.println(targetList.toString());
    }
}
Output:
[ABC, PQR, XYZ]

3. Convert Map Values to Set

let’s write a Java program to convert Map values to Set:
package net.javaguides.hibernate;

import java.util.HashMap;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class MapValuesToSetExample {

    public Set < String > mapValuesToList(Map < Integer, String > sourceMap) {
        Set < String > targetList = new HashSet < > (sourceMap.values());
        return targetList;
    }

    public static void main(String[] args) {

        MapValuesToSetExample arrayListSetExample = new MapValuesToSetExample();
        Map < Integer, String > sourceMap = new HashMap < > ();
        sourceMap.put(100, "ABC");
        sourceMap.put(101, "PQR");
        sourceMap.put(102, "XYZ");
        Set < String > targetList = arrayListSetExample.mapValuesToList(sourceMap);
        System.out.println(targetList.toString());
    }
}
Output:
[ABC, PQR, XYZ]

Similar Collections Examples [Snippet]

Learn and master Java Collections Framework at Learn Java Collections Framework
.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments