Java 8 Stream filter examples

Java 8 Stream filter
In this post,  we are going to see about Java 8 Stream filter example.
You can convert list or array to stream very easily and perform various operations on top of it.Java 8 Stream provides various methods such as map,  filter, reduce etc.
Let’s see more about Java 8 Stream filter method.

Java 8 Stream filter

As name suggests, filter method is used to filter stream on basis of criterion. You can pass lambda expressions to filter method but it should always return a boolean value. We have already seen how to pass Predicate object to filter method to filter a collection.
Lets understand more with help of example: 
Lets say you have Student class as below:

Lets say you have list of student objects in studentList and you need to filter all male students.
You can do it with the help of Stream with below code:

Here we have used stream‘s filter method to filter list and then collect the result to another list with Collectors.toList().

Java 8 filter,findAny or orElse method

You can use stream’s filter method to filter list and use findAny and orElse method based on conditions.
For example:You want to filter Student with name John, if you do not find it in the list then return null.

Lets create a main class as below:

When you run above program, you will get below output:

Male students are :[Student [id=1, name=Arpit, gender=M, age=19], Student [id=2, name=John, gender=M, age=17], Student [id=4, name=Martin, gender=M, age=21]] Student with Name john :Student [id=2, name=John, gender=M, age=17]

Let me know if you need more examples for Stream‘s filter method.


import_contacts

You may also like:

Related Posts

  • Format Instant to String in java
    02 May

    How to format Instant to String in java

    Learn about how to format Instant to String in java.

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    Table of ContentsUsing toInstant() method of Date classUsing toInstant() method of Date classUsing java.sql.Date In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    Table of ContentsUsing Instant objectUsing java.sql.Date In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using Instant object which […]

  • Convert Stream to List in java
    31 December

    Java Stream to List

    Table of ContentsUsing Collectors.toList()Using Collectors.toCollection()Using foreachFilter Stream and convert to ListConvert infinite Stream to List In this post, we will see how to convert Stream to List in java. There are multiple ways to convert Stream to List in java. Using Collectors.toList() You can pass Collectors.toList() to Stream.collect() method to convert Stream to List in […]

  • Convert LocalDateTime to Timestamp in java
    18 November

    Convert LocalDateTime to Timestamp in Java

    Table of ContentsLocalDateTimeTimestampConvert LocalDateTime to Timestamp In this post, we will how to convert LocalDateTime to Timestamp . Before learning how to convert localdatetime to timestamp, let us learn about LocalDateTime and Timestamp, and understand the importance of this conversion. LocalDateTime LocalDateTime was introcuded in Java 8. LocalDateTime can be imported time package: import java.time.LocalDateTime; […]

  • 28 August

    A In-Depth guide to Java 8 Stream API

    Table of ContentsIntroductionTypes of Stream operationsStream creationEmpty StreamCollection StreamStream.ofStream.generate()Stream.iterate()Lazy evaluationOrder of operationsPrimitive StreamsConvert Stream to IntStreamConvert IntStream to StreamEmployee classCommon intemediate operationsMap()Filter()sorted()limit()Skip()flatmap()Common terminal operationsforeachcollectReducecountallMatch()nonMatch()anyMatch()min()max()Parallel StreamsExercisesExercise 1Exercise 2Exercise 3Exercise 4Excercise 5Excercise 6Excercise 7 In this post, we will see an in-depth overview of Java 8 streams with a lot of examples and exercises. Introduction You may […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.