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

1. Overview

This short article will show how to convert the values of a Map to an Array, a List or a Set using plain Java as well as a quick Guava based example.

2. Map Values to Array

First, let’s look at converting the values of the Map into an array, using plain java:

@Test
public void givenUsingCoreJava_whenMapValuesConvertedToArray_thenCorrect() {
    Map<Integer, String> sourceMap = createMap();

    Collection<String> values = sourceMap.values();
    String[] targetArray = values.toArray(new String[0]);
}

Note, that toArray(new T[0]) is the preferred way to use the method over the toArray(new T[size]). As Aleksey Shipilëv proves in his blog post, it seems faster, safer, and cleaner.

3. Map Values to List

Next, let’s convert the values of a Map to a List – using plain Java:

@Test
public void givenUsingCoreJava_whenMapValuesConvertedToList_thenCorrect() {
    Map<Integer, String> sourceMap = createMap();

    List<String> targetList = new ArrayList<>(sourceMap.values());
}

And using Guava:

@Test
public void givenUsingGuava_whenMapValuesConvertedToList_thenCorrect() {
    Map<Integer, String> sourceMap = createMap();

    List<String> targetList = Lists.newArrayList(sourceMap.values());
}

4. Map Values to Set

Finally, let’s convert the values of the Map to a Set, using plain java:

@Test
public void givenUsingCoreJava_whenMapValuesConvertedToS_thenCorrect() {
    Map<Integer, String> sourceMap = createMap();

    Set<String> targetSet = new HashSet<>(sourceMap.values());
}

5. Conclusion

As you can see, all conversions can be done with a single line, using only the Java standard collections library.

The implementation of all these examples and code snippets can be found over on GitHub project – this is a Maven-based project, so it should be easy to import and run as it is.

Related posts:

A Custom Media Type for a Spring REST API
Spring Boot Change Context Path
Java Program to Implement Interpolation Search Algorithm
XML Serialization and Deserialization with Jackson
Java Program to Perform Complex Number Multiplication
Marker Interface trong Java
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java Program to Perform the Sorting Using Counting Sort
Java Program to Implement TreeMap API
Spring Boot - Creating Docker Image
Java Program to Generate Random Numbers Using Middle Square Method
Summing Numbers with Java Streams
Mockito and JUnit 5 – Using ExtendWith
Hướng dẫn Java Design Pattern – Visitor
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Hướng dẫn Java Design Pattern – DAO
Prevent Cross-Site Scripting (XSS) in a Spring Application
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Perform Search in a BST
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Adding Shutdown Hooks for JVM Applications
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Python Program to Convert Bytes to a String
Pagination and Sorting using Spring Data JPA
Quick Guide to Spring Controllers
Using Spring ResponseEntity to Manipulate the HTTP Response
Java Program to Implement Sparse Matrix
Exception Handling in Java
Java Program to Decode a Message Encoded Using Playfair Cipher
Generic Constructors in Java
Guide to java.util.concurrent.Future
HashMap trong Java hoạt động như thế nào?