将一个方法引用作为参数传递

huangapple go评论59阅读模式
英文:

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&lt;Double&gt; prices = List.of(1.00, 10.00, 100.00);
    List&lt;Double&gt; 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&lt;Double&gt; getPrices() { return prices; }
    public List&lt;Double&gt; 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&lt;List&lt;Double&gt;&gt; listGetter) {
    double sum = 0.0;
    for (double price : listGetter.get()) {
        sum += price;
    }
    return sum;
}

Better yet:

static double sumBy(Supplier&lt;List&lt;Double&gt;&gt; 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&lt;List&lt;Double&gt;&gt; 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&lt;Double&gt; prices = Arrays.asList(1.00, 10.00, 100.00);
List&lt;Double&gt; 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(&quot;%.2f %.2f%n&quot;, 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&lt;T&gt; functional interface is what you’re looking for:

Double sumBy(Supplier&lt;Collection&lt;Double&gt;&gt; supplier) {
  Collection&lt;Double&gt; prices = supplier.get();
}

答案4

得分: 0

使用Double sumBy(Supplier<List<Double>> doubles)

英文:

Use Double sumBy(Supplier&lt;List&lt;Double&gt;&gt; doubles)

huangapple
  • 本文由 发表于 2020年10月5日 03:18:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64198882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定