What is ForEach Method in java ?
// ForEach method is method of Iterable Interface
// It takes parameter as Consumer Functional Interface
// it is Functional Interface i.e. void accept(Type obj) has the only method
// nums.forEach(Consume obj) forEach values provide krta hai
// Consumer oos values ke sath kya krna hai woh krta hai
// n-> System.out.println(n) all automaticallt is inside the anonymous class of method void accept(Integer)
// automatically creates object of anonumous class who implements Consumer interface
// it automaticaly Detects n is type of Integer
// There we used Lambda Expression
// Simply forEach method gives values to Consumer object 'n' and it does operations like print of anything
---------------------------------------------------------------------------------------------------------------------------
### Detailed Explanation
1. **Lambda Expression**:
```java
n -> System.out.println(n)
```
- This lambda expression is a shorthand for creating an instance of an anonymous class that implements the `Consumer` interface.
- The `accept` method of the `Consumer` interface is implemented by this lambda expression, where `n` is the parameter.
2. **Anonymous Class**:
- Behind the scenes, the lambda expression `n -> System.out.println(n)` is converted to an instance of an anonymous class that implements the `Consumer` interface. It would be equivalent to:
```java
Consumer<Integer> consumer = new Consumer<Integer>() {
@Override
public void accept(Integer n) {
System.out.println(n);
}
};
```
- The `accept` method takes an `Integer` parameter `n` and performs the operation `System.out.println(n)`.
3. **Type Inference**:
- The type of `n` is inferred from the context. Since `nums` is a `List<Integer>`, the lambda parameter `n` is inferred to be of type `Integer`.
4. **forEach Method**:
- The `forEach` method takes a `Consumer` object as its parameter. This consumer is then applied to each element of the list.
- In your case:
```java
nums.forEach(n -> System.out.println(n));
```
- The `forEach` method iterates over each element in `nums` and applies the lambda expression (or the `Consumer`'s `accept` method) to each element.
### Example
Here is your code example with added comments to explain each part:
```java
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String[] args) {
// Create a list of integers
List<Integer> nums = Arrays.asList(1, 2, 4, 6, 7, 9);
// Use the forEach method to print each element
// This lambda expression is equivalent to an anonymous class implementing the Consumer interface
nums.forEach(n -> System.out.println(n));
// Equivalent code using an anonymous class
nums.forEach(new Consumer<Integer>() {
@Override
public void accept(Integer n) {
System.out.println(n);
}
});
}
}
```
### Output
Both methods will produce the same output:
```
1
2
4
6
7
9
```
### Summary
- The `forEach` method iterates over each element of the list and applies the provided `Consumer` action.
- The lambda expression `n -> System.out.println(n)` is a shorthand for an anonymous class that implements the `Consumer` interface.
- The type of `n` is inferred from the context.
- This approach simplifies the code, making it more readable and concise.
Comments
Post a Comment