Stream filter examples – Java 8

/, Java 8/Stream filter examples – Java 8

Stream filter() method :

This article is about demonstrating different Stream interface intermediate operations using filter().

filter()  returns a stream consisting of the elements of this stream that match the given predicate.

Note:
To understand Stream API and Stream interface intermediate operations See Stream API Introduction

Example with forEach() :

Program to print even numbers from list of elements.

Example with collect() :

Program to print numbers of elements those are divisible by 3 from list of elements.

Example with reduce() :

Program to print the sum of all elements those are divisible by 3 from list of elements.

Example with allMatch() :

Program to print whether all the filtered stream elements are greater than are equals to 10 or not from list of elements.

Example with anyMatch() :

Program to print whether any of the filtered stream elements are greater than 20 or not from list of elements.

Example with max() :

Program to print max element if found, returned by stream those elements are divisible by 5 from list of elements.

Example with findFirst() :

Program to print first element from stream those are divisible by 3 from list of elements.

Example with findAny() :

Program to print whether any of the element of stream found, those stream elements are divisible by 7 from list of elements.

Note:
To understand Stream API and Stream interface intermediate operations See Stream API Introduction
And also see forEach() Examples

Leave A Comment