In this tutorial, we are going to learn “How to use ‘If/Else’ conditional logic in Java 8 Streams”. We are going to achieve that with the help of Predicate and Consumer Interfaces.
Check out: takeWhile / dropWhile operations in Java 9 Stream
Iterate & ofNullable in Java 9 with Examples...!!! Share on X
In this tutorial, we are going to cover below topics:
- ‘If/Else’ condition using Consumer Interface
- ‘If’ condition using Predicate Interface with Filter method
1. ‘If/Else’ condition using Consumer Interface
‘If/Else’ logical condition can be supplied as a lambda expression using Consumer Interface in the forEach() function. It is a functional interface and has accept(Object)
as the functional method. Consumer Interface represents an operation that accepts a single input argument and returns no result.
Example
In this example, we are trying to check whether the number is Even or Odd and to do that, we are using the Consumer Interface.
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 | import java.util.Arrays; import java.util.function.Consumer; /** * Filter Even Odd numbers using If/Else logic through Consumer Interface * @author Deepak Verma * */ public class If_Else_Logic_using_Consumer_Interface { public static void main(String args[]) { var listOfIntegers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Consumer<Integer> operation = x -> { if (x % 2 == 0) { System.out.println("Even number - " + x); } else { System.out.println("Odd number - " + x); } }; listOfIntegers.stream() .forEach(operation); } } |
Output:
1 2 3 4 5 6 7 8 9 10 | Odd number - 1 Even number - 2 Odd number - 3 Even number - 4 Odd number - 5 Even number - 6 Odd number - 7 Even number - 8 Odd number - 9 Even number - 10 |
2. ‘If’ condition using Predicate Interface with Filter method
Here, we are going to use Predicate Interface which returns the boolean value. Then we will pass the output(boolean value) of the Predicate to the ‘filter‘ method which basically returns the stream elements that matches the given predicate.
Example
In this example, we are trying to checking Even/Odd numbers using Predicate Interface with ‘filter’
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 | import java.util.Arrays; import java.util.function.Predicate; /** * Filter Even Odd numbers with If logic implementation using Predicate Interface with Filter * @author Deepak Verma * */ public class If_Logic_using_Filter_method_Predicate_Interface { public static void main(String args[]) { var listOfIntegers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Predicate<Integer> isEven = x -> x % 2 == 0; Predicate<Integer> isOdd = x -> x % 2 != 0; System.out.println("List of Even Numbers: "); listOfIntegers.stream() .filter(isEven) .forEach(System.out::println); System.out.println("\nList of Odd Numbers: "); listOfIntegers.stream() .filter(isOdd) .forEach(System.out::println); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 | List of Even Numbers: 2 4 6 8 10 List of Odd Numbers: 1 3 5 7 9 |
I hope above examples could help you to understand how to implement logical conditions (if/else) using Functional Interfaces.
Cheers!
Iterate & ofNullable in Java 9 with Examples...!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Generate Prime numbers in Java 8