Java LongUnaryOperator 接口
最后修改时间:2025 年 4 月 16 日
java.util.function.LongUnaryOperator 接口表示一个函数,该函数接受一个 long 类型的参数并产生一个 long 类型的结果。它是一个函数式接口,具有一个单一的抽象方法 applyAsLong。这种特化避免了装箱/拆箱的开销。
LongUnaryOperator 是 Java 8 中添加的 Java 函数式编程实用程序的一部分。它在处理流和其他函数式操作中的原始 long 值时特别有用。该接口提供了用于组合的默认方法。
LongUnaryOperator 接口概述
LongUnaryOperator 包含一个抽象方法和几个默认方法。关键方法 applyAsLong 对输入执行操作。其他方法支持函数组合和链式调用。
@FunctionalInterface
public interface LongUnaryOperator {
long applyAsLong(long operand);
default LongUnaryOperator compose(LongUnaryOperator before);
default LongUnaryOperator andThen(LongUnaryOperator after);
static LongUnaryOperator identity();
}
上面的代码显示了 LongUnaryOperator 接口的结构。与通用的 Function 不同,它专门处理原始 long 值。该接口使用 @FunctionalInterface 注释来表明其性质。
LongUnaryOperator 的基本用法
使用 LongUnaryOperator 的最简单方法是使用 lambda 表达式。我们定义如何将输入的 long 值转换为输出的 long 值。该示例将输入值平方。
package com.zetcode;
import java.util.function.LongUnaryOperator;
public class Main {
public static void main(String[] args) {
// Define a function that squares a long value
LongUnaryOperator square = x -> x * x;
// Apply the function
System.out.println("Square of 5: " + square.applyAsLong(5));
System.out.println("Square of 12: " + square.applyAsLong(12));
// Function using arithmetic expression
LongUnaryOperator incrementAndDouble = x -> (x + 1) * 2;
System.out.println("Increment and double 3: " +
incrementAndDouble.applyAsLong(3));
}
}
此示例演示了使用 lambda 表达式的基本 LongUnaryOperator 用法。平方运算符将输入乘以自身。我们还展示了一个更复杂的操作,结合了递增和乘法。结果是原始的 long 值。
使用 andThen 进行函数组合
andThen 方法允许链接 LongUnaryOperator,其中一个的输出成为下一个的输入。这使得可以从简单的操作构建复杂的操作。
package com.zetcode;
import java.util.function.LongUnaryOperator;
public class Main {
public static void main(String[] args) {
// First function increments by 1
LongUnaryOperator increment = x -> x + 1;
// Second function multiplies by 2
LongUnaryOperator doubler = x -> x * 2;
// Compose the functions
LongUnaryOperator incrementThenDouble = increment.andThen(doubler);
System.out.println("Increment then double 5: " +
incrementThenDouble.applyAsLong(5));
System.out.println("Increment then double 10: " +
incrementThenDouble.applyAsLong(10));
}
}
此示例显示了使用 andThen 的函数组合。输入值首先增加 1,然后乘以 2。操作的顺序在链中从左到右。操作之间没有发生装箱。
使用 compose 进行函数组合
compose 方法类似于 andThen,但以相反的顺序执行函数。参数函数首先运行,然后运行原始函数。
package com.zetcode;
import java.util.function.LongUnaryOperator;
public class Main {
public static void main(String[] args) {
// Function to square a number
LongUnaryOperator square = x -> x * x;
// Function to subtract 5
LongUnaryOperator subtractFive = x -> x - 5;
// Compose in different orders
LongUnaryOperator subtractThenSquare = square.compose(subtractFive);
LongUnaryOperator squareThenSubtract = square.andThen(subtractFive);
System.out.println("Subtract 5 then square 8: " +
subtractThenSquare.applyAsLong(8));
System.out.println("Square then subtract 5 from 8: " +
squareThenSubtract.applyAsLong(8));
}
}
此示例演示了 compose 和 andThen 之间的区别。使用 compose,减法在平方之前发生。使用 andThen,平方在减法之前发生。结果显示不同的值。
在流中使用 LongUnaryOperator
LongUnaryOperator 常用与 LongStream 一起用于原始 long 值的转换。map 操作接受一个 LongUnaryOperator 来转换流元素。这避免了数值运算中的装箱开销。
package com.zetcode;
import java.util.stream.LongStream;
public class Main {
public static void main(String[] args) {
// Define transformation operations
LongUnaryOperator square = x -> x * x;
LongUnaryOperator increment = x -> x + 1;
// Apply operations in stream pipeline
LongStream.rangeClosed(1, 5)
.map(square)
.map(increment)
.forEach(System.out::println);
// Combined operation
LongUnaryOperator combined = square.andThen(increment);
System.out.println("Combined result for 3: " +
combined.applyAsLong(3));
}
}
此示例显示了 LongUnaryOperator 在 LongStream 中的用法。我们定义了平方和递增操作,并将它们应用于一系列数字。组合运算符显示了如何链接操作。所有操作都使用原始 long 值。
LongUnaryOperator Identity
LongUnaryOperator.identity 方法返回一个始终保持输入参数不变的函数。它在流处理中用作默认操作或占位符非常有用。
package com.zetcode;
import java.util.function.LongUnaryOperator;
public class Main {
public static void main(String[] args) {
// Identity function
LongUnaryOperator identity = LongUnaryOperator.identity();
System.out.println("Identity applied to 5: " +
identity.applyAsLong(5));
System.out.println("Identity applied to 100: " +
identity.applyAsLong(100));
// Using identity in stream filter
LongStream.of(10, 20, 30)
.map(LongUnaryOperator.identity())
.forEach(System.out::println);
}
}
此示例演示了 LongUnaryOperator.identity。恒等函数返回其输入值不变。在流中,当 API 需要一致性时,它可以用作无操作转换。结果与输入相同。
实际应用:数字格式化
LongUnaryOperator 可用于实际场景,如数字格式化或数据转换管道。此示例显示了货币转换操作链。
package com.zetcode;
import java.util.function.LongUnaryOperator;
public class Main {
public static void main(String[] args) {
// Conversion rates (simplified)
long usdToEurRate = 85; // 100 USD = 85 EUR
long eurToGbpRate = 90; // 100 EUR = 90 GBP
// Create conversion operators
LongUnaryOperator usdToEur = usd -> usd * usdToEurRate / 100;
LongUnaryOperator eurToGbp = eur -> eur * eurToGbpRate / 100;
// Combined conversion
LongUnaryOperator usdToGbp = usdToEur.andThen(eurToGbp);
long amountInUsd = 20000; // $200.00 in cents
System.out.println("$200 in GBP: £" +
usdToGbp.applyAsLong(amountInUsd) / 100.0);
// Formatting operator
LongUnaryOperator roundToNearest100 = x -> (x + 50) / 100 * 100;
System.out.println("Rounded 1234: " +
roundToNearest100.applyAsLong(1234));
}
}
此示例显示了 LongUnaryOperator 的实际用法。我们创建货币转换运算符并将它们链接在一起。舍入运算符演示了另一个常见的数值运算。所有计算都使用原始 long 值。
来源
在本文中,我们涵盖了 Java LongUnaryOperator 接口的基本方法和特性。理解这些概念对于在现代 Java 应用程序中进行有效的数值处理至关重要。
作者
列出所有Java教程。