Java 8 – Stream filter method with examples

In this article, we will discuss Stream filter() method in details with variety examples

1. Stream filter method :

  • This Stream method is a intermediate operation which reads stream and returns new stream after applying given predicate (or condition)
  • Predicate is Functional Interface so either we can pass Predicate class or lambda expression
  • Predicate or lambda expression is applied to each of the elements in the Stream for evaluating for the given condition
  • Stream filter() method is stateless which means it is non-interfering with other elements in the stream
  • Another intermediate operation can be applied to returned stream like map(), sorted(), distinct(), etc. after filter() intermediate operation
  • Note :- Number of elements returned in the new stream after filtering is either equal or less than the original stream elements
  • Method signature :- Stream<T> filter(Predicate<? super T> predicate)

2. Stream filter method examples :

2.1 To find all EVEN number from stream

  • A list contains first 10 natural numbers
  • We are applying condition to filter out only EVEN numbers using filter() method i.e.; Lambda expression
  • Finally, printing to console using Stream’s forEach() method
package net.bench.resources.stream.filter.example;

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

public class StreamFilterExample {

	public static void main(String[] args) {

		System.out.println("EVEN number from list of first 10 numbers : \n");

		// Integer numbers
		List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

		// filter only EVEN numbers
		Stream<Integer> stream = numbers.stream().filter(i -> i%2 == 0);

		// print to console using forEach
		stream.forEach(s -> System.out.println(s));
	}
}

Output:

EVEN number from list of first 10 numbers : 

2
4
6
8
10

2.2 To find all names with length greater than 5

  • List contains names of variable length
  • We are applying condition to filter names with length greater than 5
  • Finally, printing to console using Stream’s forEach() method
package net.bench.resources.stream.filter.example;

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

public class StreamFilterNames {

	public static void main(String[] args) {

		System.out.println("Names with length greater than 5 : \n");

		// String
		List<String> names = Arrays.asList(
				"Sachin",
				"Rahul",
				"Sehwag",
				"Anil",
				"Sourav",
				"Sunil",
				"Laxman"
				);

		// filter names with length greater than 5
		Stream<String> stream = names.stream().filter(name -> name.length() > 5);

		// print to console using forEach
		stream.forEach(str -> System.out.println(str));
	}
}

Output:

Names with length greater than 5 : 

Sachin
Sehwag
Sourav
Laxman

2.3 Stream filter() and collect() method

  • We are going to use same example which we used in last example 2.2 to filter names with length greater than 5
  • But this time instead of printing directly to console, we are going to collect to the List using Stream’s collect() method
  • Finally, iterate through new List using Stream’s forEach() method and print to console
package net.bench.resources.stream.filter.example;

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

public class StreamFilterCollectNames {

	public static void main(String[] args) {

		System.out.println("Names with length greater than 5 : \n");

		// String names
		List<String> names = Arrays.asList(
				"Sachin",
				"Rahul",
				"Sehwag",
				"Anil",
				"Sourav",
				"Sunil",
				"Laxman"
				);

		// filter names with length greater than 5
		List<String> newList = names
				.stream() // 1. get stream
				.filter(name -> name.length() > 5) // 2. intermediate operation filtering
				.collect(Collectors.toList()); // 3. collecting to List

		// print to console using forEach
		newList.stream().forEach(System.out::println);
	}
}

Output:

Names with length greater than 5 : 

Sachin
Sehwag
Sourav
Laxman

2.4 Stream filter() and map() method

  • A list contains first 10 natural numbers
  • We are applying condition to filter out only EVEN numbers using filter() method i.e.; Lambda expression
  • Then another intermediate operation to map each even number to its cube value is applied using map() method and collecting it to new List using collect() method
  • Finally, iterate through new List using forEach() method and print to console
package net.bench.resources.stream.filter.example;

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

public class StreamFilterMapMethod {

	public static void main(String[] args) {

		System.out.println("Filter EVEN numbers and Cube it : \n");

		// Integer numbers
		List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

		// filter only EVEN numbers
		List<Integer> newList = numbers
				.stream() // 1. get stream
				.filter(i -> i%2 == 0) // 2.1 intermediate operation - filtering even number
				.map(n -> n * n * n) // 2.2 intermediate operation - Square
				.collect(Collectors.toList()); // 3. collecting to List

		// print to console using forEach
		newList.forEach(s -> System.out.println(s));
	}
}

Output:

Filter EVEN numbers and Cube it : 

8
64
216
512
1000

2.5 Stream filter() with multiple conditions

  • A list contains names of variable length
  • We are applying multiple conditions to filter out names starting with capital alphabet S and having length greater than 5 using filter() method i.e.; Lambda expression
  • Then terminal operation is used to collect to a new List i .e.; collect() method of Stream
  • Finally, iterate through new List using forEach() method and print to console
package net.bench.resources.stream.filter.example;

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

public class StreamMultipleFilters {

	public static void main(String[] args) {

		System.out.println("Names starting with 'S' and"
				+ " having length greater than 5 : \n");

		// String names
		List<String> names = Arrays.asList(
				"Sachin",
				"Rahul",
				"Sehwag",
				"Anil",
				"Sourav",
				"Sunil",
				"Laxman"
				);

		// filter names with length greater than 5
		List<String> newList = names
				.stream() // 1. get stream
				.filter(s -> s.length() > 5 &amp;&amp; s.startsWith("S")) // 2 filtering
				.collect(Collectors.toList()); // 3. collecting to List

		// print to console using forEach
		newList.forEach(System.out::println);
	}
}

Output:

Names starting with 'S' and having length greater than 5 : 

Sachin
Sehwag
Sourav

References:

Happy Coding !!
Happy Learning !!

Java 8 - Stream map method with examples
Java 8 - How to create Stream