ForEach Loop with Lambda Expression
Table of Contents
- Introduction
- Key Components of ForEach Loop and Lambda Expressions
- Lambda Expression Syntax and ForEach Loop Usage
- Practical Examples
- Conclusion
Introduction
In Java, the ForEach loop is a convenient way to iterate over a collection, and when combined with Lambda expressions, it provides a powerful and concise way to perform operations on elements of a collection. This combination simplifies the code, making it more readable and efficient.
Key Points Covered:
- What is a ForEach loop in Java?
- How Lambda expressions enhance ForEach loops.
- Practical code examples from the provided project.
The ForEach method is part of the Iterable interface, and it enables developers to perform a certain action for each element of a collection. Lambda expressions, introduced in Java 8, make it easier to define such actions concisely.
Key Components of ForEach Loop and Lambda Expressions
The combination of a ForEach loop with a Lambda expression provides a streamlined way to handle operations in a functional programming style.
Component | Description |
---|---|
ForEach Loop | Iterates over elements in a collection |
Lambda Expression | Defines the action to perform on each element |
Using a ForEach loop with a Lambda expression can significantly reduce the amount of boilerplate code required to perform operations on a collection.
Lambda Expression Syntax and ForEach Loop Usage
The basic syntax of a ForEach loop with a Lambda expression can be written as follows:
1 2 3 |
collection.forEach(element -> { // Perform some action on each element }); |
This is equivalent to a traditional loop but written more concisely. Here’s how the two compare:
Traditional Loop:
ForEach with Lambda:
1 2 3 |
collection.forEach(e -> { // Perform some action on e }); |
Practical Examples
Here is an example from the project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.Arrays; import java.util.List; class Data { private String name; public Data(String name) { this.name = name; } @Override public String toString() { return "Data{" + "name='" + name + '\'' + '}'; } public String getName() { return name; } } public class Main { public static void main(String[] args) { List < Data > list = Arrays.asList(new Data("Chaand"), new Data("John"), new Data("Raj")); // Using ForEach loop with Lambda Expression list.forEach(temp -> { if (temp.getName().equals("Chaand")) { System.out.print("Founder StudyEasy: "); } System.out.println(temp.getName()); }); } } |
Explanation:
- A list of Data objects is created, each representing a person with a name.
- The ForEach loop with a Lambda expression iterates through each element in the list.
- Inside the Lambda expression, we check if the name is “Chaand” and, if so, print “Founder StudyEasy” before printing the name.
Output:
1 2 3 |
Founder StudyEasy: Chaand John Raj |
Conclusion
The ForEach loop combined with Lambda expressions in Java provides a modern, functional way of iterating over collections. It reduces code verbosity, making your code more readable and efficient. This approach is particularly useful when performing simple operations on each element of a collection.
By using Lambda expressions with ForEach loops:
- Code becomes more concise and readable.
- Functional programming concepts become easier to implement in Java.