This article will introduce how to convert a Map to a List.
1. Using the List constructor
If a Collection object of the Key and Value of the Map is passed to the constructor of ArrayList, a list with all the elements of the Set is created.
The keySet()
and values()
functions return a Collection object that contains all the keys and values respectively.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Example {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("101", "apple");
map.put("102", "kiwi");
map.put("103", "orange");
map.put("104", "banana");
map.put("105", "peach");
List<String> keyList = new ArrayList<>(map.keySet());
List<String> valueList = new ArrayList<>(map.values());
System.out.println(keyList);
System.out.println(valueList);
}
}
Output:
[101, 102, 103, 104, 105]
[apple, kiwi, orange, banana, peach]
Note that Map.keySet()
and Map.values()
return a Collection object containing the key or value elements.
And, since the Set class inherits from the Collection class, Set is also a Collection. below shows the return types of both methods.
// Map.java
Set<K> keySet();
Collection<V> values();
2. Using Stream
You can convert a Map to a List using Stream.
You can create a Stream for the key and value and convert it to a List, as follows.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Example {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("101", "apple");
map.put("102", "kiwi");
map.put("103", "orange");
map.put("104", "banana");
map.put("105", "peach");
List<String> keyList = map.keySet().stream()
.collect(Collectors.toCollection(ArrayList::new));
List<String> valueList = map.values().stream()
.collect(Collectors.toCollection(ArrayList::new));
System.out.println(keyList);
System.out.println(valueList);
}
}
Output:
[101, 102, 103, 104, 105]
[apple, kiwi, orange, banana, peach]
2.1 Filtering
If you want to filter elements by some condition, it may be a good idea to use Stream to transform Map into a List.
For example, you can convert the rest of the elements except apple
from Value into a list.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Example {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("101", "apple");
map.put("102", "kiwi");
map.put("103", "orange");
map.put("104", "banana");
map.put("105", "peach");
List<String> valueList = map.values().stream()
.filter((fruit)-> !fruit.equals("apple"))
.collect(Collectors.toCollection(ArrayList::new));
System.out.println(valueList);
}
}
Output:
[kiwi, orange, banana, peach]
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)