Java 8 Predicate example

Java 8 Predicate
Java 8 predicate is functional interface introduced in java 8.

Predicate definition

Predicate is single argument functional interface which returns true or false. It takes one argument and returns result in form of true or false.

Here is the definition of Predicate interface.

Predicate methods example

test()

This is abstract method of Predicate interface. It evaluates true if predicate matches the input argument.

Here is simple example of test() method which checks if input argument is greater than 100 or not.

Output:

is 200 greater than 100: true

You can pass Predicate as a function argument too.

and()

and() is default method which returns composite predicate that denotes logical AND of this predicate and passed predicate.

Output:

200 lies between 100 and 300: true

We have created two predicates and checked logical AND between predicate1 and predicate2

or()

or() is default method which returns composite predicate that denotes logical OR of this predicate and passed predicate.

Output:

(30 > 100) or (30 < 50) returns: true

We have created two predicates and checked logical or between predicate1 and predicate2

negate()

negate() is default method which returns predicate that denotes logical negation of this predicate.

Output:

30 is less than 100: true

We have created two predicates and checked logical or between predicate1 and predicate2

isEqual()

isEqual() is static method returns predicate that compares if two arguments are equal based on object’s equals() method.

Output:

true
false
  • predicate.test("test1") returns true because it matches with "test1" passed to isEqual method
  • predicate.test("test2") returns false because it does not match with "test1" passed to isEqual method

Please note that above comparison is done on the basis of String’s equals() method.

Filter list using Predicate

Java 8 stream’s filter method takes Predicate as a argument and can be used to filter the list with the help of predicates.
Lets say you have student class as below:

Lets create a function which will be used to filter students based on predicates.

Lets create a main class as below:

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

18 are :[Student [id=1, name=Arpit, gender=M, age=19], Student [id=4, name=Martin, gender=M, age=21]]

Female students having age < 18 are :[Student [id=3, name=Mary, gender=F, age=14], Student [id=5, name=Monica, gender=F, age=16]]

That’s all about Java 8 Predicate example


import_contacts

You may also like:

Related Posts

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    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 also: Convert LocalDate to Date in java Using toInstant() method of Date class You […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to 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 we can from Zone. Here is […]

  • Convert Stream to List in java
    31 December

    Java 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 java. Stream’s collect method performs mutable reduction operation on elements of Stream and Collectors.toList() provides […]

  • Convert LocalDateTime to Timestamp in java
    18 November

    Convert LocalDateTime to Timestamp in Java

    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; LocalDateTime is an immutable object used […]

  • 28 August

    A In-Depth guide to Java 8 Stream API

    In this post, we will see an in-depth overview of Java 8 streams with a lot of examples and exercises. Introduction You may think that Stream must be similar to InputStream or OutputStream, but that’s not the case. A Stream represents a sequence of elements supporting sequential and parallel aggregate operations. Stream does not store […]

  • Java Stream List to Map
    26 April

    Java Stream List to Map

    In this post, we will see how to convert List to Map using Stream in java 8. Collectors’s toMap() can be used with Stream to convert List to Map in java. Consider a class Named Movie which have 3 fields – id, name and genre [crayon-603a3d1077902683671831/] Create a list of movies and convert with to […]

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.