Couper
Couper

Reputation: 434

Passing a method reference as parameter

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.

Upvotes: 0

Views: 2424

Answers (4)

Andreas
Andreas

Reputation: 159250

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();
}

Upvotes: 3

kan
kan

Reputation: 28991

Use Double sumBy(Supplier<List<Double>> doubles)

Upvotes: 0

dpr
dpr

Reputation: 10972

I think the Supplier<T> functional interface is what you’re looking for:

Double sumBy(Supplier<Collection<Double>> supplier) {
  Collection<Double> prices = supplier.get();
}

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201537

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

Upvotes: 3

Related Questions