GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java IntPredicate Interface - Complete Tutorial with Examples Java IntPredicate InterfaceLast modified: April 16, 2025
The
IntPredicate Interface Overview
@FunctionalInterface
public interface IntPredicate {
boolean test(int value);
default IntPredicate and(IntPredicate other);
default IntPredicate or(IntPredicate other);
default IntPredicate negate();
}
The code above shows the structure of Basic IntPredicate UsageThe simplest way to use IntPredicate is with lambda expressions. We define the condition to test on the input value. The example checks if numbers are even. Main.java
package com.zetcode;
import java.util.function.IntPredicate;
public class Main {
public static void main(String[] args) {
// Define a predicate that tests if a number is even
IntPredicate isEven = n -> n % 2 == 0;
// Test the predicate
System.out.println("Is 4 even? " + isEven.test(4));
System.out.println("Is 7 even? " + isEven.test(7));
// Predicate for numbers greater than 10
IntPredicate isGreaterThan10 = n -> n > 10;
System.out.println("Is 15 > 10? " + isGreaterThan10.test(15));
}
}
This example demonstrates basic IntPredicate usage with lambda expressions. The isEven predicate checks if a number is divisible by 2. We test it with different values. The isGreaterThan10 shows another simple condition. Combining Predicates with AND
The Main.java
package com.zetcode;
import java.util.function.IntPredicate;
public class Main {
public static void main(String[] args) {
// Define individual predicates
IntPredicate isEven = n -> n % 2 == 0;
IntPredicate isGreaterThan10 = n -> n > 10;
// Combine with AND
IntPredicate isEvenAndGreaterThan10 = isEven.and(isGreaterThan10);
System.out.println("12: " + isEvenAndGreaterThan10.test(12));
System.out.println("8: " + isEvenAndGreaterThan10.test(8));
System.out.println("15: " + isEvenAndGreaterThan10.test(15));
}
}
This example shows predicate composition with Combining Predicates with OR
The Main.java
package com.zetcode;
import java.util.function.IntPredicate;
public class Main {
public static void main(String[] args) {
// Define individual predicates
IntPredicate isNegative = n -> n < 0;
IntPredicate isDivisibleBy3 = n -> n % 3 == 0;
// Combine with OR
IntPredicate isNegativeOrDivisibleBy3 = isNegative.or(isDivisibleBy3);
System.out.println("-5: " + isNegativeOrDivisibleBy3.test(-5));
System.out.println("9: " + isNegativeOrDivisibleBy3.test(9));
System.out.println("7: " + isNegativeOrDivisibleBy3.test(7));
}
}
This example demonstrates predicate composition with Negating a Predicate
The Main.java
package com.zetcode;
import java.util.function.IntPredicate;
public class Main {
public static void main(String[] args) {
// Original predicate
IntPredicate isPrime = n -> {
if (n < 2) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
};
// Negated predicate
IntPredicate isNotPrime = isPrime.negate();
System.out.println("Is 7 prime? " + isPrime.test(7));
System.out.println("Is 8 not prime? " + isNotPrime.test(8));
}
}
This example shows predicate negation with Using IntPredicate with IntStreamIntPredicate is commonly used with IntStream for filtering operations. The filter method accepts an IntPredicate to include only matching elements. This enables efficient processing of primitive int streams. Main.java
package com.zetcode;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
// Define a predicate for perfect squares
IntPredicate isPerfectSquare = n -> {
int sqrt = (int) Math.sqrt(n);
return sqrt * sqrt == n;
};
// Filter a range of numbers
System.out.println("Perfect squares between 1 and 100:");
IntStream.rangeClosed(1, 100)
.filter(isPerfectSquare)
.forEach(System.out::println);
}
}
This example shows IntPredicate usage with IntStream. We define a predicate to identify perfect squares, then use it to filter numbers from 1 to 100. The stream pipeline prints only numbers that satisfy the condition. Chaining Multiple PredicatesIntPredicate methods can be chained to create complex conditions from simple predicates. This approach improves code readability and maintainability. Main.java
package com.zetcode;
import java.util.function.IntPredicate;
public class Main {
public static void main(String[] args) {
// Define base predicates
IntPredicate isEven = n -> n % 2 == 0;
IntPredicate isPositive = n -> n > 0;
IntPredicate isTwoDigits = n -> n >= 10 && n <= 99;
// Chain predicates
IntPredicate complexCondition = isPositive
.and(isTwoDigits)
.and(isEven.negate());
System.out.println("15: " + complexCondition.test(15));
System.out.println("22: " + complexCondition.test(22));
System.out.println("105: " + complexCondition.test(105));
}
}
This example demonstrates chaining multiple IntPredicate operations. The complexCondition checks for positive, two-digit, odd numbers. Each test case shows how different combinations of conditions evaluate. Method Reference as IntPredicateMethod references can be used to create IntPredicate instances when existing methods match the functional interface requirements. This provides concise syntax. Main.java
package com.zetcode;
import java.util.function.IntPredicate;
public class NumberUtils {
public static boolean isPalindrome(int number) {
String s = Integer.toString(number);
return new StringBuilder(s).reverse().toString().equals(s);
}
}
public class Main {
public static void main(String[] args) {
// Method reference as IntPredicate
IntPredicate isPalindrome = NumberUtils::isPalindrome;
System.out.println("1221: " + isPalindrome.test(1221));
System.out.println("1234: " + isPalindrome.test(1234));
}
}
This example shows using a method reference as an IntPredicate. The NumberUtils.isPalindrome method checks if a number reads the same forwards and backwards. We reference this method to create a predicate instance. SourceJava IntPredicate Interface Documentation In this article, we've covered the essential methods and features of the Java IntPredicate interface. Understanding these concepts is crucial for functional programming and stream processing with primitive integers in Java. AuthorMy name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts. List all Java tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |