Java IntPredicate 接口
最后修改时间:2025 年 4 月 16 日
java.util.function.IntPredicate
接口表示接受 int 值参数的谓词。它是一个函数式接口,只有一个抽象方法 test
。 IntPredicate 常用作对基本类型 int 流的过滤操作。
IntPredicate
是 Java 8 中添加的 Java 函数式编程实用程序的一部分。它为基本类型 int 值启用行为参数化。该接口提供了用于谓词组合和链式操作的默认方法。
IntPredicate 接口概述
IntPredicate
接口包含一个抽象方法和几个默认方法。关键方法 test
在输入上评估谓词。其他方法允许谓词之间的逻辑运算。
@FunctionalInterface public interface IntPredicate { boolean test(int value); default IntPredicate and(IntPredicate other); default IntPredicate or(IntPredicate other); default IntPredicate negate(); }
上面的代码显示了 IntPredicate
接口的结构。它对基本类型 int 值进行操作,以避免装箱开销。该接口使用 @FunctionalInterface 进行注解,以表明其单一抽象方法的特性。
IntPredicate 的基本用法
使用 IntPredicate 最简单的方法是使用 lambda 表达式。我们定义要在输入值上测试的条件。该示例检查数字是否为偶数。
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)); } }
此示例演示了使用 lambda 表达式的 IntPredicate 的基本用法。isEven 谓词检查一个数字是否能被 2 整除。我们用不同的值来测试它。isGreaterThan10 显示了另一个简单的条件。
使用 AND 组合谓词
and
方法允许使用逻辑 AND 组合两个谓词。两个谓词都必须计算为 true,组合后的谓词才能返回 true。
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)); } }
此示例显示了使用 and
的谓词组合。组合谓词检查一个数字是否既是偶数又大于 10。在我们的测试用例中,只有 12 满足这两个条件。
使用 OR 组合谓词
or
方法使用逻辑 OR 组合两个谓词。组合谓词可以为 true 返回 true。
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)); } }
此示例演示了使用 or
的谓词组合。组合谓词检查一个数字是负数还是能被 3 整除。-5 和 9 满足至少一个条件,而 7 哪个条件也不满足。
否定谓词
negate
方法返回一个谓词,该谓词表示原始谓词的逻辑否定。它反转测试的结果。
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)); } }
此示例显示了使用 negate
的谓词否定。isPrime 谓词检查素数,而 isNotPrime 反转此逻辑。结果演示了否定操作。
将 IntPredicate 与 IntStream 一起使用
IntPredicate 常用作 IntStream 的过滤操作。filter 方法接受一个 IntPredicate,仅包含匹配的元素。这可以高效地处理基本类型 int 流。
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); } }
此示例显示了 IntPredicate 与 IntStream 的用法。我们定义一个谓词来识别完全平方数,然后使用它来过滤从 1 到 100 的数字。流管道仅打印满足条件的数字。
链接多个谓词
可以链接 IntPredicate 方法以从简单的谓词创建复杂的条件。这种方法提高了代码的可读性和可维护性。
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)); } }
此示例演示了链接多个 IntPredicate 操作。complexCondition 检查正的、两位数的奇数。每个测试用例显示了不同条件组合的评估方式。
方法引用作为 IntPredicate
当现有方法满足函数式接口要求时,可以使用方法引用来创建 IntPredicate 实例。这提供了简洁的语法。
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)); } }
此示例显示了将方法引用用作 IntPredicate。NumberUtils.isPalindrome 方法检查一个数字是否正反读都一样。我们引用此方法来创建一个谓词实例。
来源
在本文中,我们介绍了 Java IntPredicate 接口的基本方法和特性。理解这些概念对于在 Java 中进行函数式编程和使用基本类型 int 处理流至关重要。
作者
列出所有Java教程。