A couple of tabulate/Tabulators.groupBy examples (original) (raw)

Raab, Donald Donald.Raab at gs.com
Wed Dec 26 10:38:58 PST 2012


I updated our kata with the latest changes in the Dec. 17 binaries. Since groupBy was removed in this release I had to work through Brian's new tabulate/Tabulator approach in a couple places. Except for some type inference problems where I had to specify some extra types in various places it was not too hard to get to work. Not sure if there is an easier solution for the types here.

A simple example.

Before:

Map<String, Collection<Customer>> multimap = this.company.getCustomers().stream().groupBy(Customer::getCity);

After:

  Map<String, Collection<Customer>> multimap =
        this.company.getCustomers()
            .stream()
            .tabulate(Tabulators.<Customer, String>groupBy(Customer::getCity));

A more complex example.

Before:

    Map<Double, Collection<Customer>> multimap = this.company.getCustomers()
        .stream()
        .groupBy(customer ->
            customer.getOrders()
                .stream()
                .flatMap((Block<? super LineItem> sink, Order element) -> {element.getLineItems().forEach(sink);})
                .map(LineItem::getValue)
                .reduce(0.0, (x, y) -> Math.max(x,y)));

After:

    Map<Double, Collection<Customer>> multimap = this.company.getCustomers()
        .stream()
        .tabulate(Tabulators.<Customer, Double>groupBy((Customer customer) ->
            customer.getOrders()
                .stream()
                .<LineItem>flatMap((Block<? super LineItem> sink, Order element) -> {
                    element.getLineItems().forEach(sink);
                })
                .<Double>map(lineItem -> lineItem.getValue())
                .reduce(0.0, (x, y) -> Math.max(x, y))));


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