HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Java Stream filter() example

By Lokesh Gupta | Filed Under: Java 8

Learn to use Stream.filter(Predicate condition) method to traverse all the elements and filter out all elements which do not match a given condition through Predicate argument.

1. Stream filter() method

  • This is a intermediate operation.
  • Returns a stream consisting of the elements of this stream that match the given predicate.
  • The filter() argument shall be stateless predicate to apply to each element to determine if it should be included.
  • Predicate is a functional interface. So, we can also pass lambda expression also.
  • It returns a new stream so we can use other operations applicable to any stream.

2. Stream.filter() method syntax

The stream() method syntax is as follows:

Stream<T> filter(Predicate<? super T> condition)  

Predicate is a functional interface and represents the condition to filter out the non-matching elements from the stream.

3. Java Stream filter() examples

3.1. Find all even numbers – Lambda expression

Java example to iterate over stream of integers and print only even numbers.

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

public class Main 
{
	public static void main(String[] args) 
	{
		List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

		list.stream()
			.filter(n -> n % 2 == 0)
			.forEach(System.out::println);
	}
}

Program output.

2
4
6
8
10

3.2. Find all even numbers – Predicate class

Java example to iterate over stream of integers and print only even numbers. This example uses Predicate class in place of lambda expression, though both are same things.

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class Main 
{
	public static void main(String[] args) 
	{
		List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
		
		Predicate<Integer> condition = new Predicate<Integer>() 
		{
			@Override
			public boolean test(Integer n) {
				if (n % 2 == 0) {
					return true;
				}
				return false;
			}
		};

		list.stream().filter(condition).forEach(System.out::println);
	}
}

Program output.

2
4
6
8
10

3.3. Filter even numbers and collect into new list

We can use the collect() method to collect the stream of even numbers and converts it into a list.

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

public class Main 
{
	public static void main(String[] args) 
	{
		List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

		List<Integer> evenNumbers = list.stream()
					.filter(n -> n % 2 == 0)
					.collect(Collectors.toList());
		
		System.out.println(evenNumbers);
	}
}

Program output.

[2, 4, 6, 8, 10]

3.4. Filter even numbers and get squares

We can use the map() method to collect the stream of even numbers and then convert each number to it’s square before collecting it to a new list.

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

public class Main 
{
	public static void main(String[] args) 
	{
		List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

		List<Integer> evenNumbers = list.stream()
					.filter(n -> n % 2 == 0)
					.map(n -> n * n)
					.collect(Collectors.toList());
		
		System.out.println(evenNumbers);
	}
}

Program output.

[4, 16, 36, 64, 100]

Drop me your questions related to Java Stream filter() method in Java Stream API.

Happy Learning !!

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Leave a Reply

This comment form is under antispam protection
This comment form is under antispam protection
  Subscribe  
Notify of

Search Tutorials

Java 8 Tutorial

  • Java 8 – Introduction
  • Java 8 – forEach
  • Java 8 – Stream
  • Java 8 – Boxed Stream
  • Java 8 – Lambda Expression
  • Java 8 – Functional Interface
  • Java 8 – Method References
  • Java 8 – Default Method
  • Java 8 – Optionals
  • Java 8 – Predicate
  • Java 8 – Date Time
  • Java 8 – Iterate Directory
  • Java 8 – Read File
  • Java 8 – Write to File
  • Java 8 – WatchService
  • Java 8 – String to Date
  • Java 8 – Join Array
  • Java 8 – Base64
  • Java 8 – Exact Arithmetic
  • Java 8 – Comparator
  • Java 8 – Regex as Predicate
  • Java 8 – Join String
  • Java 8 – Difference Between Dates
  • Internal vs. External Iteration
  • Java 8- SecureRandom

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz