GS Collections Kata w/ JCF Solutions (original) (raw)

Brian Goetz brian.goetz at oracle.com
Wed Jan 16 17:55:23 PST 2013


Try compiling with -XDuseGraphInference.

On Jan 16, 2013, at 4:15 PM, Raab, Donald wrote:

Great suggestion. I just gave it a try. Max takes a comparator and returns Optional so you have to add a call to get(). I was able to change the example to the following:

Map<Double, Collection> multimap = this.company.getCustomers() .stream() .collect(Collectors.<Customer, Double>groupBy((Customer customer) -> customer.getOrders() .stream() .explode((Stream.Downstream downstream, Order order) -> { downstream.send(order.getLineItems()); }) .map(LineItem::getValue) .max(Comparators.naturalOrder()) .get())); I get an unchecked error on Comparators.naturalOrder() in IntelliJ, but adding there makes it ugly. This is the GS Collections version of the kata, which also used max(), but calls this method on a DoubleIterable: MutableListMultimap<Double, Customer> multimap = this.company.getCustomers() .groupBy(customer -> customer.getOrders() .asLazy() .flatCollect(Order::getLineItems) .collectDouble(LineItem::getValue) .max()); Unfortunately, the type inference seems to be very successful at thwarting me trying to simplify the above example using the stream library features. I would like to simplify down to this but can't seem to get it to work: Map<Double, Collection> multimap = this.company.getCustomers() .stream() .collect(groupBy((customer) -> customer.getOrders() .stream() .explode((downstream, order) -> { downstream.send(order.getLineItems()); }) .map(LineItem::getValue) .max(Comparators.naturalOrder()) .get()));

From: Joe Bowbeer [mailto:joe.bowbeer at gmail.com] Sent: Wednesday, January 16, 2013 4:21 PM To: Raab, Donald [Tech] Cc: lambda-libs-spec-experts at openjdk.java.net; Motlin, Craig P. [Tech]; Weir, John [Tech] Subject: Re: GS Collections Kata w/ JCF Solutions Thanks. Would it be better to rewrite the following using Stream.max()? .reduce(0.0, (x, y) -> Math.max(x, y)) --Joe



More information about the lambda-libs-spec-observers mailing list