If Condition in Lambda Expression Java
The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single-method interface using an expression. It is quite useful for a library's collection. It is beneficial to sort through a collection of data, iterate it over it, and extract relevant data. The use of the Lambda expression allows for the implementation of an interface with such a functional interface. Code is saved in large amounts. A lambda expression avoids this problem by allowing the implementation to be provided without redefining the method.
Just the implementation code is written here. Java lambda expressions are considered functions; hence the compiler does not produce a .class file.
If Condition
For each valid input, the lambda function would return a value. If the condition is true in this case, the block will be returned, and if it is false, the block will be returned.
1. The If Condition Combined with Predicates
We can give the condition directly to the filter() procedure as a Predicate if we just want to use "if" logic.
In the example shown, we are determining whether a number is even before printing a message.
ArrayList<Integer>numberList = new ArrayList<>(Arrays.asList(9,8,7,6,5,4));
Predicate<Integer>isEven = i ->i % 2 == 0;
numberList.stream()
.filter(isEven)
.forEach(System.out::println);
2. Consumer Implementation of the "if-else" Condition
In the forEach() function, the "if-else" condition can be implemented as a lambda expression in the form of a Consumer action.
A functional interface called Consumer has a functional method called "void accept(Object)". It stands for an operation that only takes one input argument and produces no output.
In the example provided, if a value is even, a message will be printed; if it's odd, a different message will be printed.
ArrayList<Integer>numberList
= new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6));
Consumer<Integer> action = i -> {
if (i % 2 == 0) {
System.out.println("Even number :: " + i); //Or any other user action we want to do
} else {
System.out.println("Odd number :: " + i); //Or any other user action we want to do
}
};
numberList.stream()
.forEach(action);
- Instead of just publishing the stream objects to the console, we may do anything with them, such as placing them in two different lists or passing them to other function calls.
- If necessary, set multiple if-else statements that can be written.
- Inline lambda expressions can be used to provide the Consumer implementation to the forEach() method.
Arrays.asList(-2, 1, -3, 4, 5, -6,6, 7, 0).stream()
.forEach(
i -> {
if (i == 0) {
System.out.println("Number is 0");
} else if (i> 0) {
System.out.println("Positive Number");
} else {
System.out.println("Negative Number");
}
}
);
3. Conventional if/else Logic Inside of forEach ()
Let's first establish an Integer List, and then inside the Integer stream forEach() method, utilize standard if/else logic:
List<Integer>ints = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
ints.stream()
.forEach(i -> {
if (i.intValue() % 2 == 0) {
Assert.assertTrue(i.intValue() % 2 == 0);
} else {
Assert.assertTrue(i.intValue() % 2 != 0);
}
});
The Java modulus function is used in our forEach method's if-else logic to determine whether the number is an odd or even value.
4. if/else Logic Combined With a filter()
Second, let's have a look at a more tasteful application that makes use of the Stream filter() technique:
Stream<Integer>evenIntegers = ints.stream()
.filter(i ->i.intValue() % 2 == 0);
Stream<Integer>oddIntegers = ints.stream()
.filter(i ->i.intValue() % 2 != 0);
evenIntegers.forEach(i ->Assert.assertTrue(i.intValue() % 2 == 0));
oddIntegers.forEach(i ->Assert.assertTrue(i.intValue() % 2 != 0));
In the example above, we used the Stream filter() function to divide the Integer List into two different Streams, one just for even integers and one for odd integers, in order to apply the if/else logic.
Benefits of the Lambda Expression
- Less Code: One of the main advantages of using lambda expressions is that there are fewer lines of code to write. We are aware that only a functional interface enables the use of lambda expressions. For instance, since Framework to address is a functional language, lambda expressions are simple to use.
- Support for parallel and sequential execution through the use of behaviour arguments in methods Java 8's Stream API is used to pass the functions to collection methods. Now, it is up to the collection to decide whether to handle the elements sequentially or concurrently.
- More Efficiency When doing bulk operations on collections, we can obtain higher efficiency (parallel processing) by leveraging the Stream API as well as lambda expressions. Additionally, rather than using external iteration, lambda expressions make it possible to iterate collections internally.