Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Java IntPredicate Example

Java 8 IntPredicate is a functional interface whose functional method is boolean test(int a). It can be considered a function returning true or false value.

Lokesh Gupta

November 7, 2022

Java 8
Java 8
java 8

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
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Java 8 Tutorial

  • Java 8 Features
  • Java 8 forEach
  • Java 8 Stream
  • Java 8 Boxed Stream
  • Java 8 Lambda Expression
  • Java 8 Functional Interface
  • Java 8 Method Reference
  • Java 8 Default Method
  • Java 8 Optional
  • Java 8 Predicate
  • Java 8 Regex as Predicate
  • Java 8 Date Time
  • Java 8 Iterate Directory
  • Java 8 Read File
  • Java 8 WatchService
  • Java 8 String to Date
  • Java 8 Difference Between Dates
  • Java 8 Join Array
  • Java 8 Join String
  • Java 8 Exact Arithmetic
  • Java 8 Comparator
  • Java 8 Base64
  • Java 8 SecureRandom
  • Internal vs External Iteration
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Guide to IntStream in Java

Next

Python Single and Multi-Line Comments with Shortcuts

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.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap