Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the IntPredicate functional interface in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

IntPredicate is a functional interface, which accepts an integer value argument and returns a boolean value. This interface has the following methods:

  1. test(int value)
  2. and(IntPredicate other)
  3. negate()
  4. or(IntPredicate other)

The IntPredicate interface is defined in the java.util.function package. To import the IntPredicate interface, we will check the following import statement:

import java.util.function.IntPredicate;

The test(int value) method

This method is used to test a given integer input on certain evaluations.

Syntax


boolean test(int value)

Parameters

  • int value: This is the integer input that needs to be evaluated.

Return value

This method returns the result of the evaluation, that is, either True or False.

Code

import java.util.function.IntPredicate;

public class Main{

    public static void main(String[] args) {

        // IntPredicate to check if the integer argument is even
        IntPredicate isEven = arg -> arg % 2 == 0;

        // test whether 4 is even
        System.out.println(isEven.test(4));

        // test whether 3 is even
        System.out.println(isEven.test(3));

    }
}
Test method implementation

The and(IntPredicate other) method

The and(IntPredicate other) method is used to chain multiple implementations of the IntPredicate together as a short-circuiting and logical AND operation. If any implementation returns False during the evaluation, then the subsequent implementations will not be invoked.

Syntax


default IntPredicate and(IntPredicate other)

Parameters

  • IntPredicate other: This is an IntPredicate interface that needs to be evaluated.

Return value

This method returns a composed predicate.

Code

import java.util.function.IntPredicate;;

public class Main{

    public static void main(String[] args) {

        // IntPredicate to check if the int valued argument is even
        IntPredicate isEven = arg -> arg % 2 == 0;

        // IntPredicate to check if the int valued argument is less than 100
        IntPredicate isLessThanHundred = arg -> arg < 100;

        // Composed IntPredicate
        IntPredicate composedPredicate = isEven.and(isLessThanHundred);

        // test whether 4 is even and less than 100
        System.out.println(composedPredicate.test(4));

        // test whether 103 is even and less than 100
        System.out.println(composedPredicate.test(103));

    }
}
Implementation of the "AND" operation

The negate() method

The negate() method is used to return an IntPredicate, which is a negation of the given IntPredicate.

Syntax


default IntPredicate negate()

Parameters

This method takes no parameters.

Return value

This method returns a predicate.

Code

import java.util.function.IntPredicate;;

public class Main{

    public static void main(String[] args) {

        // IntPredicate to check if the int valued argument is even
        IntPredicate isEven = arg -> arg % 2 == 0;

        // IntPredicate to get the Opposite of even using the negate method
        IntPredicate isEvenNegate = isEven.negate();

        // test negation on 4
        System.out.println(isEvenNegate.test(4));

        // test negation on 3
        System.out.println(isEvenNegate.test(3));

    }
}
Implementation of the "negate()" method

The or(IntPredicate other) method

The or(IntPredicate other) method is used to chain multiple implementations of the IntPredicate interface together as a short-circuiting and logical OR operation. If any implementation returns True during the evaluation, then the subsequent implementations will not be invoked.

Syntax


default IntPredicate or(IntPredicate other)

Parameters

  • IntPredicate other: This is an IntPredicate interface that needs to be evaluated.

Return value

This method returns a composed predicate.

Code

import java.util.function.IntPredicate;;

public class Main{

    public static void main(String[] args) {

        // IntPredicate to check if the int valued argument is even
        IntPredicate isEven = arg -> arg % 2 == 0;

        // IntPredicate to check if the int valued argument is less than 100
        IntPredicate isLessThanHundred = arg -> arg < 100;

        // Composed IntPredicate
        IntPredicate composedPredicate = isEven.or(isLessThanHundred);

        // test whether 4 is even and less than 100
        System.out.println(composedPredicate.test(4));

        // test whether 103 is even and less than 100
        System.out.println(composedPredicate.test(103));

    }
}
Implementation of the "OR" method

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring