 ## 1. Overview

In this tutorial, we're going to demonstrate how to implement if/else logic with Java 8 Streams. As part of the tutorial, we'll create a simple algorithm to identify odd and even numbers.

We can take a look at this article to catch up on the Java 8 Stream basics.

## 2. Conventional if/else Logic Within forEach()

First of all, let’s create an Integer List and then use conventional if/else logic within the Integer stream forEach() method:

``````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);
}
});``````

Our forEach method contains if-else logic which verifies whether the Integer is an odd or even number using the Java modulus operator.

## 3. if/else Logic With filter()

Secondly, let's look at a more elegant implementation using the Stream filter() method:

``````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));``````

Above we implemented the if/else logic using the Stream filter() method to separate the Integer List into two Streams, one for even integers and another for odd integers.

## 4. Conclusion

In this quick article, we’ve explored how to create a Java 8 Stream and how to implement if/else logic using the forEach() method.

Furthermore, we learned how to use the Stream filter method to achieve a similar result, in a more elegant manner.

Finally, the complete source code used in this tutorial is available over on Github.

### I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Inline Feedbacks praveen
1 year ago

Which if the 2 options is more performant? Eric Martin
1 year ago

After some testing, the forEach() example has a bit better performance. Keep in mind that the two examples are really just meant to show two different methods of using if/else logic in streams. Stream.forEach() and Stream.filter() solve two different problems. George
1 year ago

I would propose a more elegant solution to even and odds problem and also more memory efficient :

Map<Boolean, List> partitions = numbers.stream().collect(Collectors.partitioningBy(n -> (n % 2) == 0)); Eric Martin
1 year ago

That is definitely another solution. I don’t necessarily agree with the ‘memory efficiency’ part because we now have three solutions to a similar problem, but not the same problem. Evan Kellogg
1 year ago

Hi Eugen,

Although the stream filter option might look more elegant, it would be significantly less efficient in an application as it is looping over the same data twice. Personally I believe that using the first example or the good ol’ enhanced for loop would be more efficient solution for an if/else problem. Eric Martin
1 year ago