LongUnaryOperator是一个java.util.function函数接口。此函数接口接受单个长值操作数并生成长值结果。LongUnaryOperator接口可以用作lambda表达式和方法引用的赋值目标。它包含一个抽象方法:applyAsLong()、一个静态方法identity()和两个默认方法:andThen()和compose()。
@FunctionalInterface public interface LongUnaryOperator long applyAsLong(long operand); }
import java.util.function.LongUnaryOperator; public class LongUnaryOperatorTest { public static void main(String args[]) { LongUnaryOperator getSquare = longValue -> { // lambda long result = longValue * longValue; System.out.println("Getting square: " + result); return result; }; LongUnaryOperator getNextValue = longValue -> { // lambda long result = longValue + 1; System.out.println("Getting Next value: " + result); return result; }; LongUnaryOperator getSquareOfNext = getSquare.andThen(getNextValue); LongUnaryOperator getNextOfSquare = getSquare.compose(getNextValue); long input = 10L; System.out.println("Input long value: " + input); System.out.println(); long squareOfNext = getSquareOfNext.applyAsLong(input); System.out.println("Square of next value: " + squareOfNext); System.out.println(); long nextOfSquare = getNextOfSquare.applyAsLong(input); System.out.println("Next to square of value: " + nextOfSquare); } }
输出结果
Input long value: 10 Getting square: 100 Getting Next value: 101 Square of next value: 101 Getting Next value: 11 Getting square: 121 Next to square of value: 121