Java IntPredicate Example

Lokesh Gupta

Java IntPredicate the interface is a Predicate with one int-valued argument. The IntPredicate can be considered an operator or function that returns a value either true or false based on certain evaluations of the argument int value.

1. How to Create and Use IntPredicate?

Technically IntPredicate is a functional interface whose functional method is boolean test(int a).

Suppose we want to write a function to check if a given number is an odd number then we can do it in one line statement. Optionally, we can write a function (more specifically a predicate) that will test the argument number and check if it is an odd number.

In case of an odd number, the method will return ‘true‘ else ‘false‘.

IntPredicate isOddNumber = argument -> argument % 2 == 1;

Now we can use the Predicate with Stream of Integer-type elements.

IntStream stream = IntStream.range(1, 10); 

List<Integer> oddNumbers = stream.filter(isOdd)
            .boxed()
            .collect(Collectors.toList());    //[1, 3, 5, 7, 9]

We can also use the predicate to test individual integer values as well.

System.out.println( isOdd.test(9) );  //true

System.out.println( isOdd.test(10) ); //false

2. Using Complex IntPredicate

Suppose we do not have a very simple condition to evaluate. Rather we have a few independent conditions which shall be evaluated in combination. For example, we want to find all odd numbers less than 20 which are prime numbers as well.

Here, we can define two predicates independently and combine their usage to check if both conditions satisfy. To combine two predicates, use the below functions:

  • IntPredicate and(IntPredicate other) – Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
  • IntPredicate or(IntPredicate other) – Composed predicate represents a short-circuiting logical OR of this predicate and another.

It also provides IntPredicate negate() method which returns a predicate that represents the logical negation of this predicate.

import java.util.List;
import java.util.function.IntPredicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
 
public class IntPredicateExample 
{
  public static void main(String[] args) 
  {
    IntPredicate isOdd = argument -> argument % 2 == 1;
     
    IntStream stream = IntStream.range(1, 20); 
     
    List<Integer> oddPrimes = stream.filter( isOdd.and(IntPredicateExample::isPrime) )
              .boxed()
              .collect(Collectors.toList());
     
    System.out.println(oddPrimes);
  }
   
  public static boolean isPrime(int i) 
  {
        IntPredicate isDivisible = index -> i % index == 0;
        return i > 1 && IntStream.range(2, i).noneMatch(isDivisible);
  }
}

Program output.

[3, 5, 7, 11, 13, 17, 19]

3. Conclusion

In this short Java tutorial, we introduced the IntPredicate class that is used to test the integer type values. We learned to combine multiple predicates to create complex predicates for more complex conditions.

Happy Learning !!

Comments

Subscribe
Notify of
guest
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.