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

16 thoughts on “Java – How to use Stream Filter in Java 8 with List & Array Examples”

  1. 128851 351366Get started with wales ahead nearly every planking. Ones wales truly are a compilation of huge planks one particular depth advisors definitely may be the identical to the entire hull planking even so with even larger density to successfully thrust outward beyond the planking. planking 425240

  2. 316580 36308When I initially commented I clicked the -Notify me when new comments are added- checkbox and now each time a remark is added I get four emails with exactly the same comment. Is there any manner you possibly can take away me from that service? Thanks! 649239

  3. 75657 951338Hoping to go into business venture world-wide-web Indicates revealing your products or services furthermore companies not only to ladies locally, but nevertheless , to several prospective clients in which are online in most cases. e-wallet 408181

  4. 894009 74861You completed quite a few nice points there. I did a search on the issue and discovered nearly all men and women will have the same opinion with your weblog. 854383

  5. 367587 500004you are in point of fact a very good webmaster. The website loading velocity is remarkable. It seems that youre doing any distinctive trick. In addition, The contents are masterpiece. youve done a terrific activity on this subject! 812839

  6. 623607 739092This really is a excellent topic to speak about. Sometimes I fav stuff like this on Redit. I dont feel this would be the best to submit though. Ill take a look about your internet site though and submit something else. 139670

Got Something To Say:

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