如何使用 lambda 表达式在 Java 中实现 IntBinaryOperator?
IntBinaryOperator 是 Java 8 中java.util.function 包中的函数接口。此接口期望接受两个类型为 int 的参数作为输入,并生成一个int 类型的结果。IntBinaryOperator 可用作lambda 表达式或方法 引用的赋值目标。它仅包含一个抽象方法:applyAsInt()。
语法
@FunctionalInterface
public interface IntBinaryOperator {
int applyAsInt(int left, int right)
}示例
import java.util.function.*;
public class IntBinaryOperatorTest {
public static void main(String[] args) {
IntBinaryOperator test1 = (a, b) -> a + b; // lambda expression
System.out.println("Addition of two parameters: " + test1.applyAsInt(10, 20));
IntFunction test2 = new IntFunction() {
@Override
public IntBinaryOperator apply(int value) {
return new IntBinaryOperator() {
@Override
public int applyAsInt(int left, int right) {
return value * left * right;
}
};
}
};
System.out.println("Multiplication of three parameters: " + test2.apply(10).applyAsInt(20, 30));
}
}输出
Addition of two parameters: 30 Multiplication of three parameters: 6000
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP