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

java-8-stream-filter-examples-feature-image


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.

Signature:

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:

Conclusion

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


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

  1. 752924 355823You may be websites successful individuals, it comes effortlessly, therefore you also earn you see, the jealousy of all the ones a great deal of journeymen surrounding you can have challenges within this challenge. motor movers 790456

  2. 625754 293227You created some decent points there. I looked on the internet for that problem and located a lot of people will go in addition to with the internet site. 171647

  3. 475850 224444The next time I learn a weblog, I hope that it doesnt disappoint me as considerably as this one. I mean, I do know it was my choice to read, nonetheless I truly thought youd have something attention-grabbing to say. All I hear is actually a bunch of whining about something that you could fix for those that werent too busy in search of attention. 487247

  4. 274862 937953Thank you a great deal for sharing this with all people you actually recognize what you are speaking about! Bookmarked. Please in addition speak more than with my internet website =). We could have a hyperlink alternate arrangement among us! 400132

  5. 719734 807726The the next occasion I read a weblog, I actually hope so it doesnt disappoint me about brussels. Come on, man, Yes, it was my option to read, but I just thought youd have some thing fascinating to state. All I hear can be plenty of whining about something which you could fix in case you werent too busy searching for attention. 766081

  6. 8241 38854Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what youre talking about, why waste your intelligence on just posting videos to your site when you could be giving us something informative to read? 164110

  7. 419995 211037hello admin, your internet site pages pattern is simple and clean and i like it. Your articles are remarkable. Remember to keep up the excellent work. Greets.. 349756

  8. 496333 9897One can undertake all sorts of advised excursions with assorted limousine functions. Various offer great courses and many can take clients for just about any ride your bike over the investment banking area, or even for a vacation to new york. ??????? 577894

  9. 455561 755541Superb editorial! Would like took pleasure the particular following. Im hoping to learn to read a good deal much more of you. Theres no doubt that you possess tremendous awareness and even imagination. I happen to be quite highly fascinated employing this critical details. 902680

  10. 994047 118363Hey, you used to write fantastic, but the last couple of posts have been kinda boringK I miss your great writings. Past couple of posts are just a bit out of track! come on! 509379

  11. 605564 760174This is the proper blog for anybody who hopes to learn about this subject. You know a complete lot its almost tough to argue along (not that I in fact would wantHaHa). You undoubtedly put a whole new spin for a subject thats been written about for years. Excellent stuff, just fantastic! 160842

  12. 482243 634123Oh my goodness! a fantastic write-up dude. Thanks a good deal Nonetheless We are experiencing trouble with ur rss . Do not know why Not able to sign up to it. Maybe there is anybody obtaining identical rss dilemma? Anyone who knows kindly respond. Thnkx 86567

  13. 94845 532708Interested in start up a online business on line denotes revealing your service also providers not only to humans within your town, nevertheless , to numerous future prospects which are cyberspace on several occasions. pays everyday 331701

  14. 106192 172555Pretty section of content. I just stumbled upon your weblog and in accession capital to assert that I get in fact enjoyed account your blog posts. Any way I will probably be subscribing to your augment and even I achievement you access consistently speedily. 369187

  15. 364347 560310Im so pleased to read this. This is the kind of manual that needs to be given and not the accidental misinformation thats at the other blogs. Appreciate your sharing this best doc. 121237

  16. 293548 803172We offer the most effective practical and most applicable solutions. All our Sydney plumbers are experienced and qualified and are able to speedily assess your dilemma and locate the most effective solution. 311503

  17. 692483 653599Good post. It does shed some light on the concern. By the for those interested in binary options can get an exclusive binary options bonus. 465383

  18. 640179 430089For anybody who is interested in enviromentally friendly items, may possibly possibly surprise for you the crooks to keep in mind that and earn under a holder just because kind dissolved acquire various liters to important oil to make. day-to-day deal livingsocial discount baltimore washington 26211

  19. 785060 861275Hello, Neat post. There can be a difficulty along with your site in internet explorer, could test thisK IE nonetheless is the marketplace leader and a large portion of men and women will leave out your excellent writing due to this issue. 16802

Got Something To Say:

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

*