In Java, the Stream class was introduced as a part of the Java 8 Streams API that provides a functional way of processing collections of objects, i.e. a sequence of elements that can be processed in a functional manner.
With the help of the Stream class, you can perform various operations on collections like filtering, mapping, sorting, reducing, and more, without modifying the original collection.
Convert Map<K,V> as List<E>
To convert a Map to a List in Java 8 using Stream,
Step 1: We can use the entrySet() method of the Map interface to get a set of the map's key-value pairs.
Step 2: We can then use the stream() method to convert the set to a stream,
Step 3: We use the map() method to transform each entry into an object of your desired type
Step 4: Lastly, we can make use of the collect() method to collect the stream into a List.
Example:package org.example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
*
* Code Example in Java 8 to convert
* Map to List using Stream API
*
*
*/
public class MapToList {
public static void main(String[] args) {
Map<String,String> cityTempMap = new HashMap<>();
cityTempMap.put("NYC","34");
cityTempMap.put("Chicago","32");
cityTempMap.put("Austin","42");
cityTempMap.put("Huston","31");
cityTempMap.put("Ohio","30");
//Map to List
List<String> cities = cityTempMap
.entrySet() //Step 1:entrySet() to get K-V Pair
.stream() //Step 2: Convert Set to Stream
.map(entry -> entry.getKey()) // Step 3: Use map() to transform
.collect(Collectors.toList()); // Step 4: Collect Stream as List
System.out.println(cities);
}
}
Output:
[Chicago, NYC, Ohio, Austin, Huston]
Facing issues? Have Questions? Post them here! I am happy to answer!
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
- Shortcut: Move Cursor at the start or end of Line or file in Visual Studio Code (VS Code) - Shortcuts
- Fix- Microsoft Word Pages Appear Black - Microsoft
- Create SharePoint list from Excel spreadsheet and import table - SharePoint
- Android : Exception raised during rendering: action_bar API 22 - Android
- How to Toggle Dark and Light Mode on macOS Sonoma 14 - MacOS
- error CAML Query containing special characters - SharePoint
- Stop Automatic Opening of Music App on Mac when I connect Earphones - MacOS
- Bash Commands to Display File Contents - Bash