Java IntUnaryOperator 接口
最后修改时间:2025 年 4 月 16 日
java.util.function.IntUnaryOperator 接口表示对单个 int 值操作数执行操作并产生 int 值结果。它是一个函数式接口,具有单个抽象方法 applyAsInt。
IntUnaryOperator 是 Java 8 中添加的 Java 函数式编程实用程序的一部分。它是 UnaryOperator 在 int 基本类型上的特化,避免了自动装箱的开销。该接口提供了用于函数组合的默认方法。
IntUnaryOperator 接口概述
IntUnaryOperator 包含一个抽象方法和两个默认方法。关键方法 applyAsInt 对输入执行操作。其他方法支持函数组合。
@FunctionalInterface
public interface IntUnaryOperator {
int applyAsInt(int operand);
default IntUnaryOperator compose(IntUnaryOperator before);
default IntUnaryOperator andThen(IntUnaryOperator after);
static IntUnaryOperator identity();
}
以上代码显示了 IntUnaryOperator 接口的结构。它专门针对 int 基本类型值进行操作。该接口使用 @FunctionalInterface 进行注解,以表明其单一抽象方法的性质。
IntUnaryOperator 的基本用法
使用 IntUnaryOperator 的最简单方法是使用 lambda 表达式。我们定义了如何将输入 int 转换为输出 int。该示例对输入进行平方。
package com.zetcode;
import java.util.function.IntUnaryOperator;
public class Main {
public static void main(String[] args) {
// Define an operator that squares its input
IntUnaryOperator square = x -> x * x;
// Apply the operator
System.out.println("Square of 5: " + square.applyAsInt(5));
System.out.println("Square of 12: " + square.applyAsInt(12));
// Operator using method reference
IntUnaryOperator abs = Math::abs;
System.out.println("Absolute of -8: " + abs.applyAsInt(-8));
}
}
此示例演示了使用 lambda 和方法引用的 IntUnaryOperator 的基本用法。平方运算符接受一个 int 并返回其平方。我们将其应用于不同的值。方法引用为现有方法提供了简洁的语法。
使用 andThen 进行函数组合
andThen 方法允许链式运算符,其中一个运算符的输出成为下一个运算符的输入。这使得可以从简单操作创建复杂操作。
package com.zetcode;
import java.util.function.IntUnaryOperator;
public class Main {
public static void main(String[] args) {
// First operator increments by 1
IntUnaryOperator increment = x -> x + 1;
// Second operator doubles the value
IntUnaryOperator doubler = x -> x * 2;
// Compose the operators
IntUnaryOperator incrementThenDouble = increment.andThen(doubler);
System.out.println("Increment then double 5: " + incrementThenDouble.applyAsInt(5));
System.out.println("Increment then double 10: " + incrementThenDouble.applyAsInt(10));
}
}
此示例显示了使用 andThen 的运算符组合。输入首先增加 1,然后翻倍。操作顺序在链中从左到右。结果显示了转换序列。
使用 compose 进行函数组合
compose 方法类似于 andThen,但以相反的顺序执行运算符。参数运算符先运行,然后是原始运算符。
package com.zetcode;
import java.util.function.IntUnaryOperator;
public class Main {
public static void main(String[] args) {
// Operator to square a number
IntUnaryOperator square = x -> x * x;
// Operator to subtract 5
IntUnaryOperator subtractFive = x -> x - 5;
// Compose in different orders
IntUnaryOperator subtractThenSquare = square.compose(subtractFive);
IntUnaryOperator squareThenSubtract = square.andThen(subtractFive);
System.out.println("Subtract 5 then square 10: " + subtractThenSquare.applyAsInt(10));
System.out.println("Square then subtract 5 from 10: " + squareThenSubtract.applyAsInt(10));
}
}
此示例演示了 compose 和 andThen 之间的区别。使用 compose,减法在平方之前发生。使用 andThen,平方在减法之前发生。结果差异很大。
将 IntUnaryOperator 与 Streams 一起使用
IntUnaryOperator 通常与 Java Streams 一起使用,用于 int 基本类型转换。map 操作接受一个 IntUnaryOperator 来转换流元素。这使得数据处理更加高效。
package com.zetcode;
import java.util.Arrays;
import java.util.function.IntUnaryOperator;
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
// Operator to calculate factorial
IntUnaryOperator factorial = n -> {
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
};
// Apply operator in stream
Arrays.stream(numbers)
.map(factorial)
.forEach(System.out::println);
}
}
此示例显示了在 Streams 中使用 IntUnaryOperator。我们定义了一个阶乘运算符,并通过 map 将其应用于每个流元素。结果为每个转换后的值打印出来。基本类型流避免了装箱开销。
IntUnaryOperator Identity
IntUnaryOperator.identity 方法返回一个始终返回其输入参数的运算符。当操作需要一个运算符但您希望保持值不变时,这很有用。
package com.zetcode;
import java.util.function.IntUnaryOperator;
public class Main {
public static void main(String[] args) {
// Identity operator
IntUnaryOperator identity = IntUnaryOperator.identity();
System.out.println("Identity applied to 7: " + identity.applyAsInt(7));
// Practical use in conditional processing
IntUnaryOperator processor = x -> x > 10 ? x * 2 : identity.applyAsInt(x);
System.out.println("Process 5: " + processor.applyAsInt(5));
System.out.println("Process 15: " + processor.applyAsInt(15));
}
}
此示例演示了 IntUnaryOperator.identity。身份运算符返回其输入不变。在条件处理中,它充当不满足转换条件的值的无操作。
组合多个 IntUnaryOperator
我们可以组合多个 IntUnaryOperator 来创建复杂的转换。这种方法促进了代码重用和操作的模块化设计。
package com.zetcode;
import java.util.function.IntUnaryOperator;
public class Main {
public static void main(String[] args) {
// Basic operators
IntUnaryOperator addTen = x -> x + 10;
IntUnaryOperator triple = x -> x * 3;
IntUnaryOperator halve = x -> x / 2;
// Complex combined operator
IntUnaryOperator complexOp = addTen.andThen(triple).andThen(halve);
System.out.println("Complex operation on 4: " + complexOp.applyAsInt(4));
System.out.println("Complex operation on 10: " + complexOp.applyAsInt(10));
// Alternative combination
IntUnaryOperator altComplexOp = halve.compose(triple).compose(addTen);
System.out.println("Alternative operation on 4: " + altComplexOp.applyAsInt(4));
}
}
此示例显示了组合多个 IntUnaryOperator。我们创建基本操作并将它们链接在一起。complexOp 加上 10,乘以 3,然后将结果减半。操作的顺序非常重要。
来源
在本文中,我们介绍了 Java IntUnaryOperator 接口的基本方法和特性。理解这些概念对于在 Java 应用程序中进行基于基本类型的有效函数式编程至关重要。
作者
列出所有Java教程。