Java IntPredicate

Introduction

In Java, the IntPredicate interface is a functional interface that represents a predicate (boolean-valued function) with a single int-valued argument. It is part of the java.util.function package and is used for testing conditions involving int values.

Table of Contents

  1. What is IntPredicate?
  2. Methods and Syntax
  3. Examples of IntPredicate
  4. Real-World Use Case
  5. Conclusion

1. What is IntPredicate?

IntPredicate is a functional interface that accepts an int and returns a boolean result. It is commonly used for evaluating conditions or filtering data.

2. Methods and Syntax

The main methods in the IntPredicate interface are:

  • boolean test(int value): Evaluates this predicate on the given argument.
  • default IntPredicate and(IntPredicate other): Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
  • default IntPredicate or(IntPredicate other): Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
  • default IntPredicate negate(): Returns a predicate that represents the logical negation of this predicate.

Syntax

IntPredicate intPredicate = (int value) -> {
    // condition on value
    return result;
};

3. Examples of IntPredicate

Example 1: Checking if a Number is Even

import java.util.function.IntPredicate;

public class EvenCheckExample {
    public static void main(String[] args) {
        // Define an IntPredicate that checks if a number is even
        IntPredicate isEven = (value) -> value % 2 == 0;

        boolean result = isEven.test(10);
        System.out.println("Is 10 even? " + result);
    }
}

Output:

Is 10 even? true

Example 2: Using and

import java.util.function.IntPredicate;

public class AndExample {
    public static void main(String[] args) {
        // Define predicates for checking if a number is even and greater than 5
        IntPredicate isEven = (value) -> value % 2 == 0;
        IntPredicate isGreaterThanFive = (value) -> value > 5;

        IntPredicate isEvenAndGreaterThanFive = isEven.and(isGreaterThanFive);

        boolean result = isEvenAndGreaterThanFive.test(8);
        System.out.println("Is 8 even and greater than 5? " + result);
    }
}

Output:

Is 8 even and greater than 5? true

Example 3: Using or

import java.util.function.IntPredicate;

public class OrExample {
    public static void main(String[] args) {
        // Define predicates for checking if a number is even or greater than 5
        IntPredicate isEven = (value) -> value % 2 == 0;
        IntPredicate isGreaterThanFive = (value) -> value > 5;

        IntPredicate isEvenOrGreaterThanFive = isEven.or(isGreaterThanFive);

        boolean result = isEvenOrGreaterThanFive.test(3);
        System.out.println("Is 3 even or greater than 5? " + result);
    }
}

Output:

Is 3 even or greater than 5? false

Example 4: Using negate

import java.util.function.IntPredicate;

public class NegateExample {
    public static void main(String[] args) {
        // Define a predicate for checking if a number is even
        IntPredicate isEven = (value) -> value % 2 == 0;

        IntPredicate isOdd = isEven.negate();

        boolean result = isOdd.test(5);
        System.out.println("Is 5 odd? " + result);
    }
}

Output:

Is 5 odd? true

4. Real-World Use Case: Filtering Even Numbers from a List

In applications, IntPredicate can be used to filter even numbers from a list.

import java.util.function.IntPredicate;
import java.util.stream.IntStream;

public class FilterEvenNumbers {
    public static void main(String[] args) {
        // Define an IntPredicate to check for even numbers
        IntPredicate isEven = (value) -> value % 2 == 0;

        IntStream.of(1, 2, 3, 4, 5, 6)
                .filter(isEven)
                .forEach(value -> System.out.println("Even number: " + value));
    }
}

Output:

Even number: 2
Even number: 4
Even number: 6

Conclusion

The IntPredicate interface is used in Java for evaluating conditions involving int values. It simplifies filtering and logical operations, enhancing code clarity and maintainability. Using IntPredicate can lead to cleaner and more efficient code, especially in functional programming contexts.

Comments