
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: April 4, 2025
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.
This article is part of the “Java – Back to Basic” series here on Baeldung.
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.
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());
}
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());
}
As you can see, all conversions can be done with a single line, using only the Java standard collections library.