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


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

  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 7149096645Você conhece o jeito mais tranquilo de aumentar de elo em LOL? Absolutamente, não é se irritando em partidas rankeds. A melhor alternativa é pedir um elo job, um serviço oferecido por um player high elo, com melhor série de vitórias e entre os profissionais do jogo, que aumentará o elo da sua conta.9129126028

  11. 9980109008No low elo costumam ficar os jogadores que não conseguem se empenhar com o game, por essa causa, a taxa de desistências no meio das partidas (os AFKs), de mensagens de ira e de feedings é bem maior.4911587688

  12. 4832021708 No League of Legends, se um jogador percebe que outro pode ser um elo boost ou um smurf (apelido aqueles que têm contas em elos mais altos, mas jogam em contas com elos mais baixos) é possível informar e fazer com que aquela conta seja testada. Se isso ocorrer, você pode ser banido. 2619458496

Got Something To Say:

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

*