Note: This works with Java 8 or above!

In order to convert a Stream to a List, make use of the of the toList() method form the Collectors util class when collecting the Stream.
Example:
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamToListExamples {
public static void main(String[] args) {
Stream<Integer> streamOfIntegers = Stream.of(1,2,4,5,6,7,8,9,10);
//Stream -> List
List<Integer> listOfIntegers = streamOfIntegers.collect(Collectors.toList());
System.out.println(listOfIntegers);
Stream<String> streamOfStrings = Stream.of("A","B","C","D","E");
//Stream -> List
List<String> listOfStrings = streamOfStrings.collect(Collectors.toList());
System.out.println(listOfStrings);
}
}
Output:
[1, 2, 4, 5, 6, 7, 8, 9, 10]
[A, B, C, D, E]
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Git: Delete Branch Locally and Remotely at Origin - Git
- Delete Android Studio Projects - Android-Studio
- How to Stream Photos and Videos iPhone to your TV - iOS
- Python Program To Calculate Simple Interest (SimpleInterest.py) - Python
- Can't Run SDK Manager find_java.bat issue - Android
- How to change TextView or EditText Text Color on Focus and on Press - Android
- Spotlight Search file path location on Mac OS X Mavericks or Yosemite - Mac-OS-X
- How to install Microsoft Teams on Mac using Terminal (Silently) - Teams