🎓 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
1. Introduction
This tutorial demonstrates how to use the forEach() method of the Java Stream API. forEach() is a terminal operation that helps in iterating over each element of the stream, allowing actions to be performed on each element. This method simplifies operations on collections and is a key part of functional programming in Java.
Key Points
1. forEach() is used for iterating over each element of a stream to execute a provided action.
2. It is a terminal operation, which means it consumes the stream and you cannot use the stream after forEach() is called.
3. forEach() should be used for operations that do not affect the underlying data source (non-interfering).
2. Program Steps
1. Create a Stream of elements.
2. Apply the forEach() method to perform an action on each element.
3. Demonstrate actions like printing elements or performing calculations.
3. Code Program
import java.util.stream.Stream;
public class StreamForEachExample {
public static void main(String[] args) {
// Stream of elements
Stream<String> fruitStream = Stream.of("apple", "banana", "cherry", "date", "elderberry");
// Apply forEach to print each element
fruitStream.forEach(fruit -> System.out.println("Fruit: " + fruit));
// Stream of numbers for a sum calculation
Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5);
// Mutable container for the sum
final int[] sum = {0};
// Apply forEach to calculate sum of elements
numberStream.forEach(number -> sum[0] += number);
System.out.println("Sum of numbers: " + sum[0]);
}
}
Output:
Fruit: apple Fruit: banana Fruit: cherry Fruit: date Fruit: elderberry Sum of numbers: 15
Explanation:
1. Stream.of("apple", "banana", "cherry", "date", "elderberry") creates a stream of fruit names.
2. fruitStream.forEach(fruit -> System.out.println("Fruit: " + fruit)) iterates over each fruit in the stream and prints its name.
3. Stream.of(1, 2, 3, 4, 5) creates a stream of numbers.
4. The sum of the numbers is calculated using a forEach loop, where each element is added to a sum accumulator (sum[0] += number).
5. The use of an array sum to store the accumulator value is a workaround to allow modification of the variable within the lambda expression.
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