GS Collections Kata w/ JCF Solutions (original) (raw)
Arul Dhesiaseelan aruld at acm.org
Wed Jan 16 19:19:51 PST 2013
- Previous message: GS Collections Kata w/ JCF Solutions
- Next message: FYI: LongStream and DoubleStream have been pushed
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Try this (works with b73):
Map<Double, Collection<Customer>> 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 used this in a similar context here [1]
-Arul
[1] https://github.com/aruld/java-oneliners/blob/master/src/main/java/com/aruld/oneliners/Item10.java
On Wed, Jan 16, 2013 at 2:15 PM, Raab, Donald <Donald.Raab at gs.com> 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
- Previous message: GS Collections Kata w/ JCF Solutions
- Next message: FYI: LongStream and DoubleStream have been pushed
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the lambda-libs-spec-observers mailing list