Java – How to use Stream Filter in Java 8 with List & Array Examples


Java 8 provides an extremely powerful abstract concept Stream with many useful mechanics for consuming and processing data in Java Collection. In the tutorial, We will use lots of examples to explore more the helpful of Stream API with filtering function on the specific topic: “Filter Collection with Java 8 Stream”.

What will we do?

  • How to filter List with traditional approach?
  • Use Java 8 Stream to filter List and Array Objects
  • Apply filter() function with other util functions of Stream API in practice

Now let’s do examples for more details!

Related posts:
Java – How to find an element in a Java List Object by Examples
Java 8 Streams

Java Filter List by Traditional Approach with Looping

Before Java 8, for filtering a List Objects, we use the basic approach with looping technical solution.

Looping Example – Filter Integer List

– Example: How to get all even number in a list?

Looping Example – Filter String List

– Example: How to get all string that contains “Java” sub-string in a string List?

Looping Example – Filter Custom Object List

– Create a Customer class:

– Looping through a List to filter objects having age >= 18:

Java 8 Stream Filter Examples

With the a powerful concept Stream of Java 8, we can use the filter() method to filtering each element from a list.


Stream Filter Example with Integer List

– Example:

In above example code, the filter() method returns a stream. So we need an util collect() method to transform stream elements into another container such as a List.

Stream Filter Example with String List

– Example:

In the above code, We use map() function of Java 8 Stream to combine with filter() to help transforms each string after filter to Uppercase.

Stream Filter Example with Custom List

– Example:

In above example, we use forEach() terminal operation to performs an action System.out.println() for each stream element.

We can also combine filter stream with sorted() method:

Java 8 Stream Filter with Multi-Condition

Assume We need filter all customers having age less than 30 and be an odd number. How to do it?

-> We have 2 approaches:

Approach 1: apply multiple checking for multi-conditions in only one filter() method.

Approach 2: Use multiple filter() method together:

Parallel & Sequential Streams Filter Example

Stream provides a parallel and sequential API processing, so We can combine it with filter() method to leverage the multiple cores on your computer for performance processing data.

– See below example:

Stream Filter Array Objects with Java 8

For using stream to filter objects in Array with Java 8, We do 2 steps:

  • Create Stream from Array Objects.
  • Apply the filtering in stream for Array Objects as the same way we had done with above List Objects.

Example Code:


We had done how to use Java 8 Stream Filter with Examples:

  • How to Filter Java List before Java 8 with looping approach
  • Apply Stream Filter to Integer, String and Custom Object List
  • Combine filter() method with other methods of Java 8 Stream such as: map(), sorted(), forEach()
  • Apply Stream Filter to Java Array
  • Explore how to use parallel & sequential streams with filter() method

Happy Learning! See you later!

By grokonez | April 2, 2019.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *