Predicates in Java8 with Code examples


Predicates are often used in mathematics which means , a function which takes an argument and returns a boolean.In Java8 , they are functional interfaces and meant to do the same and they can also be implemented using Lambda.

Predicates are Boolean valued functions which takes one value as an argument, performs some logic and returns true or false.

Predicates diagrammatic representation

Java Doc:

Interface Predicate<T>
Type Parameters:
T - the type of the input to the predicate
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

Code Example:

Check out this example , I am using Predicate to determine if the length of a string is greater than 5 or not.I am using a Lambda expression for the same.

package com.ppkcodes.examples;

import java.util.function.Predicate;

public class PredicateExample {
    public static void main(String[] args) {
        Predicate<String> strLength= (str)->str.length()>5;
        System.out.println("Length of the string1 greater than 5? "+strLength.test("This is a Loong sentence"));
        System.out.println("Length of the string2 greater than 5? "+strLength.test("Short"));

Predicate<String> strLength= (str)->str.length()>5;

Code result:
Length of the string1 greater than 5? true
Length of the string2 greater than 5? false

To use Predicates as filters

You can also use them to filter out a collection, such as a list based on a criteria.

In the below example , there is a an integer list called ‘numbers‘ and few integers.I want to show numbers which are greater than 10 and hence I am using a predicate here.

package com.ppkcodes.examples;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class PredicateFilter {
    public static void main(String[] args) {
        List<Integer> numbers= Arrays.asList(0,2,3,33,5,1,11,7,23,77,98,65);

Interview questions on predicates in Java 8:

Ill keep updating below section with interview questions.If you find few questions please share it with me and Ill add it here.

How do you implement strategy pattern using Predicates

A strategy pattern is a very common design pattern for letting you choose an implementation at runtime.Let’s apply this pattern to validate an input string.

Using Predicates for strategy pattern implementation.
  • The interface ValidationMethod represents an algorithm.
  • There are 2 concrete implementations for it , namely isAllLowerCase and IsNumber.
  • Validator is a client which uses Validate(Strategy) object.

ValidationMethod is your functional interface .

package com.codingbrains.examples;

  public interface ValidationMethod {
  boolean check(String s);

There are 2 strategies to validate the String. One is to check if the string consists of lower case letters only.

package com.codingbrains.examples;

public class isAllLowerCase implements ValidationMethod {

  public boolean check(String str) {
    return str.matches("[a-z]+");

Another strategy is to check if the string consists of only number or not.

package com.codingbrains.examples;

public class IsNumber implements ValidationMethod {

  public boolean check(String str) {
    return str.matches("\\d+");

Both validation strategies are used in the client Validator.

package com.codingbrains.examples;

public class Validator {

  private ValidationMethod validate;

  public Validator(ValidationMethod val) {
    this.validate = val;

  public boolean check(String str) {
    return validate.check(str);

  public static void main(String[] args) {
    //Without Lambda expression - start
    Validator numValidator=new Validator(new IsNumber()) ;
    System.out.println("Are all numbers "+numValidator.check("111"));

    Validator isLowerCase=new Validator(new isAllLowerCase());
    System.out.println("Are all lower cases "+isLowerCase.check("aaAa"));
    //Without Lambda expression - end

    //With Lambda expression - start
    Validator numberValidatorLambda=new Validator((String str)->str.matches("\\d"));
    System.out.println("Lambda Expression : Are all numbers "+numberValidatorLambda.check("111"));

    Validator isLowerCaseLambda=new Validator((String str)->str.matches("[a-z]"));
    System.out.println("Lambda Expression : Are all lower case letters "+isLowerCaseLambda.check("aaAa"));
    //With Lambda expression - end


Did you notice that the above code shows 2 flavors of implementing the strategy pattern using predicates.One of them is using Lambda and another does not use Lambda.

What are the different primitive variants of predicates in java8?

Below are the primitive variants :

IntPredicate, LongPredicate, DoublePredicate

To know more about the difference between a generic vs primitive predicates , read the post Primitive variants of functional interfaces.

What is the difference between a predicate and function?

A Predicate<T> takes an input of type T and always returns a boolean.

A Function<T,R> takes an input of type T and returns an object of type R.

What is a Predicate ?

1.A Predicate is a functional interface introduced in Java 8.

2.A Predicate<T> takes an input of type T and always returns either true or false.

3. It has a test() method , which can be used to evaluate a predicate.

4.It was introduced in java.util.function package.

What is the difference between a Predicate, Supplier and Consumer?

All three are functional interfaces introduced as part of Java8.

1.A Predicate takes an input and returns a boolean.

2.A Supplier does not take any input but returns a result.

3.A Consumer takes an input but does not return any result.

You may also like...

Leave a Reply

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