Java – IntPredicate

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

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

1. IntPredicate – Simple example

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

In case of odd number, method will return ‘true’ else return value will be ‘false’.

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;

		//1. Use predicate directly
		
		System.out.println( isOdd.test(9) );	//true
		System.out.println( isOdd.test(10) );	//false
		
		//2. Use predicate in filters
		
		IntStream stream = IntStream.range(1, 10); 
		
		List<Integer> oddNumbers = stream.filter(isOdd)
						.boxed()
						.collect(Collectors.toList());
		
		System.out.println(oddNumbers);
	}
}

Program output.

true
false
[1, 3, 5, 7, 9]

2. Complex IntPredicate

Suppose we do not have a very simple condition to evaluate. Rather we have few independent conditions which shall be evaluated in combination.

For example, we want to find all odd numbers less than 20 which are prime number as well.

Here, we can define tho conditions independently and them combine their usage on a number to check if both conditions satisfy. To combine two predicates, use 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]

Drop me your questions in comments.

Happy Learning !!

Ref : Java Doc

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

1 thought on “Java – IntPredicate”

  1. Its good example isOdd.and(isPrime) for explaining complex predicate.But, when we use isOdd.and(isPrime) to get primes, we are missing 2. As its a prime number, but not add.So,The better solution would be..
    public static boolean isPrimeAny(int i)
    {
    IntPredicate isDivisible = index -> i % index == 0;
    return i > 2 ? (!IntStream.range(2, i).anyMatch(isDivisible)):true;
    }

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.