企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 2.2.4 函数式接口列表 | 函数式接口 | 函数描述符 | 原始类型特化 | | ---- | ---- | ---- | | `Predicate<T>` | `T->boolean` | `BooleanSupplier` `IntSupplier` `LongSupplier` `DoubleSupplier` | | `UnaryOperator<T>` | `T->T` | `IntUnaryOperator` `LongUnaryOperator` `DoubleUnaryOperator` | | `BinaryOperator<T>` | `(T, T)->T` | `IntBinaryOperator` `LongBinaryOperator` `DoubleBinaryOperator` | | `BiPredicate<L, R>` | `(L, R)->boolean` | | | `BiConsumer<T, U>` | `(T, U)->void` | `ObjIntConsumer<T>` `ObjLongConsumer<T>` `ObjDoubleConsumer<T>` | | `BiFunction<T, U, R>` | `(T, U)->R` | `ToIntBiFunction<T, U>` `ToLongBiFunction<T, U>` `ToDoubleBiFunction<T, U>` **原始类型特化**为了避免装箱拆箱而设置的专门版本,例如`Predicate<Integer>`使用过程中的装箱拆箱操作,可以通过`IntPredicate`来避免: ```java import java.util.Arrays; import java.util.function.IntPredicate; public class IntPredicateTest { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5}; IntPredicate oddPredicate = i -> (i % 2 == 1); int[] oddNumbers = filter(numbers, oddPredicate); System.out.println(Arrays.toString(oddNumbers)); } static int[] filter(int[] numbers, IntPredicate action) { if (numbers == null || numbers.length == 0) { return null; } int[] result = new int[numbers.length]; int count = 0; for (int i = 0; i < numbers.length; i++) { if (action.test(numbers[i])) { result[count++] = numbers[i]; } } return Arrays.copyOfRange(result, 0, count); } } ```