Java 8 Stream API filter() examples

1. Overview


In this Stream tutorial, We will learn today what is Stream filter introduced in Java 8. Java 8 has a Stream API and it has a filter() method which takes the Predicate Functional Interface as an argument. filter() method is a part of Intermediate Operations which is used in the middle of streams and returns a Stream as output.

Java 8 Stream filter() example




2. Stream API filter() method syntax


Below is the syntax from API.

Stream filter​(Predicate predicate)

This method returns a stream consisting of the elements of this stream that match the given predicate. Predicate means just a condition that applied to all the values in the Stream. We are still eligible to perform many operations on the returned Stream.

3. Java 8 Stream filter() examples


Now, We will see various example programs on the filter() method. In all the examples, filter stream will be converted to a List using Collectors.toList() utility method.


3.1 Print all odd numbers - Lambda expression


Java example program to iterate over a stream of integers and pick only odd numbers from it.

package com.java.w3schools.blog.java.program.to.java8;

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

/**
*
* Java program to print only odd numbers using Stream filter() api.
*
* @author JavaProgramTo.com
*
*/

public class StreamFilterLambda {

public static void main(String[] args) {

// creating a stream from 1 to 10 numbers.
Stream stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);

// fitering only odd numbers
List oddNumbers = stream.filter(num -> num % 2 == 1).collect(Collectors.toList());

// printing
oddNumbers.forEach(value -> System.out.println(value));

}

}

Output:

1
3
5
7
9

3.2 Print all odd numbers - Custom Predicate Functional Interface


Let us create a Predicate Functional interface that checks the given number is odd or not. This can be used in many places rather than writing this logic in multiple places.

package com.java.w3schools.blog.java.program.to.java8;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
*
* Java program to print only odd numbers with predicate using Stream filter()
* api.
*
* @author JavaProgramTo.com
*
*/

public class StreamFilterPredicate {

public static void main(String[] args) {

// creating a stream from 1 to 10 numbers.
Stream stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);

// Custom predicate to check odd number.
Predicate oddPredicate = new Predicate() {

@Override
public boolean test(Integer t) {

if (t % 2 == 1) {
return true;
}

return false;
}
};

// filtering only odd numbers using custom predicate
List oddNumbers = stream.filter(oddPredicate).collect(Collectors.toList());

// printing
oddNumbers.forEach(value -> System.out.println(value));

}

}

Output:


1
3
5
7
9

3.3 Filtering odd numbers and generating square numbers



package com.java.w3schools.blog.java.program.to.java8;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
*
* Java program to find odd numbers and print its squares.
*
* @author JavaProgramTo.com
*
*/

public class StreamFilterSqueres {

public static void main(String[] args) {

// creating a stream from 1 to 10 numbers.
Stream stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);

// Custom predicate to check odd number.
Predicate oddPredicate = new Predicate() {

@Override
public boolean test(Integer t) {

if (t % 2 == 1) {
return true;
}

return false;
}
};

// filtering only odd numbers using custom predicate and generating square
// numbers.
List oddNumbers = stream.filter(oddPredicate).map(value -> value * value).collect(Collectors.toList());

// printing
oddNumbers.forEach(value -> System.out.println(value));

}

}

Output:

1
9
25
49
81

3.4 Filtering odd numbers and generating its cubic values


package com.java.w3schools.blog.java.program.to.java8;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
*
* Java program to find odd numbers and print its cubic values.
*
* @author JavaProgramTo.com
*
*/

public class StreamFilterCubic {

public static void main(String[] args) {

// creating a stream from 1 to 10 numbers.
Stream stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);

// filtering only odd numbers using custom predicate and generating square
// numbers.
List oddNumbers = stream.filter(n -> n % 2 == 1).map(value -> value * value * value)
.collect(Collectors.toList());

// printing
oddNumbers.forEach(value -> System.out.println(value));

}

}

Output:

1
27
125
343
729

4. Conclusion


In this article, We have seen what is Java 8 Stream filter and a few example programs using filter() method. And how to collect the output of filter into List.




0 Comments