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:

Receive email by java client
Giới thiệu Google Guice – Dependency injection (DI) framework
Ignore Null Fields with Jackson
JUnit5 Programmatic Extension Registration with @RegisterExtension
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Python Program to Check If a List is Empty
Java Program to Implement Sorted Circular Doubly Linked List
Spring Boot - Service Components
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Giới thiệu Design Patterns
Spring Autowiring of Generic Types
Cơ chế Upcasting và Downcasting trong java
Read an Outlook MSG file
Hướng dẫn Java Design Pattern – Facade
Lớp lồng nhau trong java (Java inner class)
Python Set symmetric_difference_update()
HashMap trong Java hoạt động như thế nào?
Chuyển đổi từ HashMap sang ArrayList
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
So sánh HashMap và Hashtable trong Java
String Operations with Java Streams
Java Program to Implement LinkedBlockingDeque API
Interface trong Java 8 – Default method và Static method
Spring Boot - Tomcat Port Number
Java Program to Compute Cross Product of Two Vectors
A Guide to the ResourceBundle
Filtering a Stream of Optionals in Java
Java Program to Implement AttributeList API
ETags for REST with Spring
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu