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:

How to Remove the Last Character of a String?
Java Program to Perform integer Partition for a Specific Case
Java Program to Solve a Matching Problem for a Given Specific Case
Java Program to Generate Randomized Sequence of Given Range of Numbers
Service Registration with Eureka
Apache Commons Collections Bag
Java Program to Check whether Graph is a Bipartite using DFS
Một số ký tự đặc biệt trong Java
Java Collections Interview Questions
Hướng dẫn sử dụng Java Generics
Giới thiệu Google Guice – Dependency injection (DI) framework
Java Program to Implement Pairing Heap
Java Program to Implement Double Order Traversal of a Binary Tree
Tránh lỗi NullPointerException trong Java như thế nào?
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Convert Hex to ASCII in Java
An Introduction to Java.util.Hashtable Class
Java Program to Implement the Hill Cypher
Show Hibernate/JPA SQL Statements from Spring Boot
Java Program to Implement Dijkstra’s Algorithm using Set
Format ZonedDateTime to String
Merging Two Maps with Java 8
The Difference Between Collection.stream().forEach() and Collection.forEach()
An Intro to Spring Cloud Contract
Java 14 Record Keyword
Guide to the Java TransferQueue
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Các nguyên lý thiết kế hướng đối tượng – SOLID
Hướng dẫn Java Design Pattern – Dependency Injection
Java Program to Implement Heap Sort Using Library Functions
Check If a String Is Numeric in Java
HttpClient Timeout