Javatpoint Logo
Javatpoint Logo

Java Lambda foreach

Java Lambda foreach is a new feature introduced in Java 8. It allows developers to write more concise and readable code by reducing the amount of boilerplate code required for iterating through collections. Java Lambda foreach is a method that is used to iterate through collections and apply a specific action to each element in the collection. The Java Lambda foreach method is part of the Java Stream API, which is a powerful tool for working with collections of data. In this article, we will explore the Java Lambda foreach method and its features.

Java Lambda foreach syntax

The syntax for using the Java Lambda foreach method is quite simple. It takes a lambda expression as its argument, which specifies the action to be performed on each element in the collection.

The syntax for using the Java Lambda foreach method is as follows:

In the above syntax, the "collection" is the collection of elements that we want to iterate through. The "forEach" method is used to iterate through the collection, and the "element -> { }" is the lambda expression that specifies the action to be performed on each element.

The "element" variable in the lambda expression represents each element in the collection. The code inside the curly braces specifies the action to be performed on each element.

Let's look at an example of using the Java Lambda foreach method. Suppose we have a list of integers that we want to iterate through and print each element to the console. We can use the following code:

NumberPrinter.java

Output:

1
2
3
4
5

Java Lambda foreach with Streams

The Java Lambda foreach method can also be used with streams, which allows for more powerful operations on collections. A stream is a sequence of elements that can be processed in parallel or sequentially. To use the Java Lambda foreach method with streams, we first create a stream from the collection using the "stream" method. We can then use the forEach method to iterate through the stream and perform some action on each element.

Here's an example of using the Java Lambda foreach method with streams:

EvenNumberPrinter.java

Output:

2
4

In the above code, we create a list of integers and initialize it with some values. We then create a stream from the list using the "stream" method. We use the "filter" method to filter out the odd numbers and only keep the even numbers. We then use the forEach method to iterate through the stream and print each even number to the console.

Java Lambda foreach with Maps

The Java Lambda foreach method can also be used with maps, which allows for iterating through the key-value pairs in the map.

To use the Java Lambda foreach method with maps, we first obtain a set of the map's entries using the "entrySet" method. We can then use the forEach method to iterate through the set and perform some action on each entry.

Here's an example of using the Java Lambda foreach method with maps:

AgeMapPrinter.java

Output:

John: 25
Mary: 30
Bob: 35

In the above code, we create a map that maps people's names to their ages. We then obtain a set of the map's entries using the "entrySet" method. We use the forEach method to iterate through the set and print out each key-value pair to the console.

Benefits of Using Java Lambda foreach

There are several benefits to using the Java Lambda foreach method:

  1. Concise code: The Java Lambda foreach method allows for more concise code by reducing the amount of boilerplate code required for iterating through collections.
  2. Readability: The lambda expression used in the Java Lambda foreach method makes the code more readable by clearly specifying the action to be performed on each element.
  3. Efficiency: The Java Lambda foreach method can be used with parallel streams to process elements in parallel, which can improve performance on multi-core processors.
  4. Flexibility: The Java Lambda foreach method can be used with a variety of collections, including lists, sets, maps, and more.

In Summary, The Java Lambda foreach method is a powerful tool for iterating through collections and performing actions on each element. It allows for more concise and readable code, and can be used with a variety of collections, including parallel streams. By using the Java Lambda foreach method, developers can write more efficient and flexible code, which can improve performance and maintain.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA