Stream.reduce(Float,BinaryOperator) BinaryOperator 指的是哪个功能接口方法?
Stream.reduce(Float,BinaryOperator) BinaryOperator refers which functional interface method?
例如,
List<Product> productsList = new ArrayList<Product>();
productsList.add(new Product(1,"HP Laptop",25000f));
productsList.add(new Product(2,"Dell Laptop",30000f));
productsList.add(new Product(3,"Lenevo Laptop",28000f));
productsList.add(new Product(4,"Sony Laptop",28000f));
productsList.add(new Product(5,"Apple Laptop",90000f));
Float totalPrice = productsList.stream()
.map(product->product.price)
.reduce(0.0f,(sum, price)->sum+price);
System.out.println(totalPrice);
这里(sum, price)->sum+price
指的是哪个功能接口?
查看 Stream
Javadoc:
T reduce(T identity, BinaryOperator<T> accumulator)
(sum, price)->sum+price
在您的示例中实现了 BinaryOperator<Float>
。
这个功能接口有一个方法接受两个相同类型的参数(Float
在你的例子中)和 returns 一个相同类型的结果。
因为你提到了 "functional interface method": reduce
is actually calling apply
of BiFunction
(from which BinaryOperator
extends).
例如,
List<Product> productsList = new ArrayList<Product>();
productsList.add(new Product(1,"HP Laptop",25000f));
productsList.add(new Product(2,"Dell Laptop",30000f));
productsList.add(new Product(3,"Lenevo Laptop",28000f));
productsList.add(new Product(4,"Sony Laptop",28000f));
productsList.add(new Product(5,"Apple Laptop",90000f));
Float totalPrice = productsList.stream()
.map(product->product.price)
.reduce(0.0f,(sum, price)->sum+price);
System.out.println(totalPrice);
这里(sum, price)->sum+price
指的是哪个功能接口?
查看 Stream
Javadoc:
T reduce(T identity, BinaryOperator<T> accumulator)
(sum, price)->sum+price
在您的示例中实现了 BinaryOperator<Float>
。
这个功能接口有一个方法接受两个相同类型的参数(Float
在你的例子中)和 returns 一个相同类型的结果。
因为你提到了 "functional interface method": reduce
is actually calling apply
of BiFunction
(from which BinaryOperator
extends).