Pages

Saturday, December 11, 2021

Java 8 Streams if else logic

1. Overview

In this tutorial, We'll learn how to use if/else statements in java 8 streams.

If you are new to the java 8 stream,  It is recommended to read the in-depth on the basics of java 8 streams.

First, we will see the simple to find the even numbers from the given list of numbers.

Next, we will write the java 8 examples with the forEach() and streams filter() method.

Java 8 Streams if else logic



2. Java Conventional If Else condition


In the below example, a List with the integers values is created. Next, we run the for loop from index 0 to list size - 1. Take each index value and check the number is even or not using if-else condition.

If the if-condition is satisfied then it is an even number else it is an odd number.

Example 1
package com.javaprogramto.java8.streams.filter.ifelse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class IfElseExample {

	public static void main(String[] args) {

		List<Integer> numbersList = Arrays.asList(10, 13, 15, 20, 24, 16, 17, 100);

		List<Integer> evenList = new ArrayList<>();
		List<Integer> oddList = new ArrayList<>();

		for (int index = 0; index < numbersList.size(); index++) {
			if (numbersList.get(index) % 2 == 0) {
				evenList.add(numbersList.get(index));
			} else {
				oddList.add(numbersList.get(index));
			}
		}

		System.out.println("Even numbers list - " + evenList);
		System.out.println("Odd numbers list - " + oddList);
	}

}

Output
Even numbers list - [10, 20, 24, 16, 100]
Odd numbers list - [13, 15, 17]

3. Java 8 Streams If Else condition In forEach()

In java 8, stream api is added with the forEach() method and inside this, we can add the if-else conditions.

But, forEach() is called on the streams.

Example 2
package com.javaprogramto.java8.streams.filter.ifelse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Java8StreamForEachExample {

	public static void main(String[] args) {

		List<Integer> numbersList = Arrays.asList(10, 13, 15, 20, 24, 16, 17, 100);

		List<Integer> evenList = new ArrayList<>();
		List<Integer> oddList = new ArrayList<>();
		numbersList.stream().forEach(number -> {
			if (number % 2 == 0) {
				evenList.add(number);
			} else {
				oddList.add(number);
			}

		});

		System.out.println("Even numbers list - " + evenList);
		System.out.println("Odd numbers list - " + oddList);
	}
}

Output
Even numbers list - [10, 20, 24, 16, 100]
Odd numbers list - [13, 15, 17]




4. Java 8 Streams If Else condition


By using traditional for loop and if-else conditions, we could get the odd and even numbers into separate lists but we had to write many lines of code.

But, with the new concepts of java 8 streams, the full logic can be reduced to two lines to get the separate even and odd numbers lists.

Use the java 8 stream filter() method to get the equivalent functionality of the traditional if condition.

Example 3

look at the below code how the filter() method takes the predicate functional interface. 

The Predicate is the functional interface that does the job of if condition. But this predicate statement should be passed to the filter() method so that the predicate function is evaluated.

package com.javaprogramto.java8.streams.filter.ifelse;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Java8StreamIfElseExample {

	public static void main(String[] args) {

		List<Integer> numbersList = Arrays.asList(10, 13, 15, 20, 24, 16, 17, 100);

		List<Integer> evenList = numbersList.stream().filter(number -> number % 2 == 0).collect(Collectors.toList());
		List<Integer> oddList = numbersList.stream().filter(number -> number % 2 == 1).collect(Collectors.toList());

		System.out.println("Even numbers list - " + evenList);
		System.out.println("Odd numbers list - " + oddList);
	}
}


Output
Even numbers list - [10, 20, 24, 16, 100]
Odd numbers list - [13, 15, 17]

We can see the same output as in section 2. 

Java 8 streams syntax completely reduces the boilerplate code.


4. Java 8 Streams Multiple If conditions


Actually, we can call the filter() multiple times on the streams. So, if you have multiple conditions in your use case, either you can add all conditions in the single predicate statement or you can make calls to the filter() method as many times you want.


Example 4

The below example get the even numbers that are greater than 20.
Here, we have two conditions - first one is number is greater than 20 and second is checking the even number.

package com.javaprogramto.java8.streams.filter.ifelse;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Java8StreammultipleIfExample {

	public static void main(String[] args) {

		List<Integer> numbersList = Arrays.asList(10, 13, 15, 20, 24, 16, 17, 100);

		List<Integer> evenList = numbersList.stream()
						.filter(number -> number > 20)
						.filter(number -> number % 2 == 0)
						.collect(Collectors.toList());

		System.out.println("Even numbers list - " + evenList);
	}
}

Output
Even numbers list - [24, 100]


6. Conclusion


In this article, we've seen how to use if-else and multiple if conditions with java 8 streams.




No comments:

Post a Comment

Please do not add any spam links in the comments section.