🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The toList() method in Java, part of the java.util.stream.Stream interface, is used to collect the elements of a stream into an immutable list. This method is useful when you need to convert a stream into a list for further processing or manipulation.
Table of Contents
- Introduction
toList()Method Syntax- Understanding
toList() - Examples
- Basic Usage
- Real-World Use Case
- Conclusion
Introduction
The toList() method is a terminal operation that collects the elements of a stream into an immutable list. This method is available starting from Java 16 and provides a convenient way to convert streams into lists.
toList() Method Syntax
List<T> toList()
Parameters:
- This method does not take any parameters.
Returns:
- An immutable
Listcontaining the elements of the stream.
Throws:
- This method does not throw any exceptions.
Understanding toList()
The toList() method collects all elements of the stream into an immutable list. This method is useful for scenarios where you need a list representation of the stream elements.
Examples
Basic Usage
To demonstrate the basic usage of toList(), we will create a Stream of integers and collect them into a list.
Example
import java.util.List;
import java.util.stream.Stream;
public class ToListExample {
public static void main(String[] args) {
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
// Use toList() to collect elements into a list
List<Integer> list = stream.toList();
// Print the elements of the list
list.forEach(System.out::println);
}
}
Output:
1
2
3
4
5
Real-World Use Case
Converting a Stream of Employees to a List
In real-world applications, the toList() method can be used to convert a stream of complex objects, such as employees, into a list for further processing.
Example
import java.util.List;
import java.util.stream.Stream;
public class ToListRealWorldExample {
static class Employee {
String name;
double salary;
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return name + ": " + salary;
}
}
public static void main(String[] args) {
Stream<Employee> employees = Stream.of(
new Employee("Alice", 50000),
new Employee("Bob", 60000),
new Employee("Charlie", 70000)
);
// Use toList() to collect elements into a list
List<Employee> employeeList = employees.toList();
// Print the elements of the list
employeeList.forEach(System.out::println);
}
}
Output:
Alice: 50000.0
Bob: 60000.0
Charlie: 70000.0
Conclusion
The Stream.toList() method is used to collect the elements of a stream into an immutable list. This method is particularly useful for converting streams into lists for further processing or manipulation. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring that elements are collected into lists as needed.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment