英文:
Passing a method reference as parameter
问题
在这种情况下:
public class Order {
List<Double> prices = List.of(1.00, 10.00, 100.00);
List<Double> pricesWithTax = List.of(1.22, 12.20, 120.00);
Double sumBy(/* 方法引用 */) {
Double sum = 0.0;
for (Double price : /* 方法引用 */) {
sum += price;
}
return sum;
}
public List<Double> getPrices() { return prices; }
public List<Double> getPricesWithTax() { return pricesWithTax; }
}
如何以使以下方式调用声明 `sumBy` 方法:
Order order = new Order();
var sum = order.sumBy(order::getPrices);
var sumWithTaxes = order.sumBy(order::getPricesWithTax);
**我不使用Java 8 API进行求和,因为我的目标仅是理解如何传递方法引用。**
英文:
In a case like this:
public class Order {
List<Double> prices = List.of(1.00, 10.00, 100.00);
List<Double> pricesWithTax = List.of(1.22, 12.20, 120.00);
Double sumBy(/* method reference */) {
Double sum = 0.0;
for (Double price : /* method reference */) {
sum += price;
}
return sum;
}
public List<Double> getPrices() { return prices; }
public List<Double> getPricesWithTax() { return pricesWithTax; }
}
how can I declare the sumBy
method in a way that can be called like this:
Order order = new Order();
var sum = order.sumBy(order::getPrices);
var sumWithTaxes = order.sumBy(order::getPricesWithTax);
I'm not using the Java 8 API for the sum because my goal is only to understand how pass a method reference.
答案1
得分: 3
Your 2 methods take no argument and return an object, so that fits the Supplier.get()
method.
Don't use Double
for the sum
variable, since that will auto-box and auto-unbox way too much.
Method can be static
since it doesn't use any fields or other methods of the class.
static double sumBy(Supplier<List<Double>> listGetter) {
double sum = 0.0;
for (double price : listGetter.get()) {
sum += price;
}
return sum;
}
Better yet:
static double sumBy(Supplier<List<Double>> listGetter) {
return listGetter.get().stream().mapToDouble(Double::doubleValue).sum();
}
英文:
Your 2 methods take no argument and return an object, so that fits the Supplier.get()
method.
Don't use Double
for the sum
variable, since that will auto-box and auto-unbox way too much.
Method can be static
since it doesn't use any fields or other methods of the class.
static double sumBy(Supplier<List<Double>> listGetter) {
double sum = 0.0;
for (double price : listGetter.get()) {
sum += price;
}
return sum;
}
Better yet:
static double sumBy(Supplier<List<Double>> listGetter) {
return listGetter.get().stream().mapToDouble(Double::doubleValue).sum();
}
答案2
得分: 2
你似乎想要一个类似于 Supplier
的东西,就像这样:
Double sumBy(Supplier<List<Double>> f) {
Double sum = 0.0;
for (Double price : f.get()) {
sum += price;
}
return sum;
}
你的 List.of
语法给我带来了错误。所以我做了以下修改:
List<Double> prices = Arrays.asList(1.00, 10.00, 100.00);
List<Double> pricesWithTax = Arrays.asList(1.22, 12.20, 120.00);
然后我进行了测试:
public static void main(String[] args) throws IOException {
Order order = new Order();
double sum = order.sumBy(order::getPrices);
double sumWithTaxes = order.sumBy(order::getPricesWithTax);
System.out.printf("%.2f %.2f%n", sum, sumWithTaxes);
}
输出结果为:
111.00 133.42
英文:
You seem to want a Supplier
like
Double sumBy(Supplier<List<Double>> f) {
Double sum = 0.0;
for (Double price : f.get()) {
sum += price;
}
return sum;
}
Your List.of
syntax was giving me errors. So I did
List<Double> prices = Arrays.asList(1.00, 10.00, 100.00);
List<Double> pricesWithTax = Arrays.asList(1.22, 12.20, 120.00);
Then I tested like
public static void main(String[] args) throws IOException {
Order order = new Order();
double sum = order.sumBy(order::getPrices);
double sumWithTaxes = order.sumBy(order::getPricesWithTax);
System.out.printf("%.2f %.2f%n", sum, sumWithTaxes);
}
Outputs
111.00 133.42
答案3
得分: 0
我认为 Supplier<T>
函数接口是你正在寻找的:
Double sumBy(Supplier<Collection<Double>> supplier) {
Collection<Double> prices = supplier.get();
}
英文:
I think the Supplier<T>
functional interface is what you’re looking for:
Double sumBy(Supplier<Collection<Double>> supplier) {
Collection<Double> prices = supplier.get();
}
答案4
得分: 0
使用Double sumBy(Supplier<List<Double>> doubles)
。
英文:
Use Double sumBy(Supplier<List<Double>> doubles)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论