4

What is Java stream API alternative to LambdaJ indexing? Let's say I have code like this

List<Product> products = ...
Map<Month, Product> productsOnMonths = Lambda.index(products, Lambda.on(Product.class).getMonth());

Where I know that every product has unique month attribute.

Jan Krakora
  • 2,500
  • 3
  • 25
  • 52

1 Answers1

4
 products.stream().collect(Collectors.toMap(Product::getMonth, s -> s));

The difference here is that Collectors.toMap can take a third argument that says how to merge two entries when they are the same; I don't think lambdaj offers that

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • Very elegant. Finally I used a version with identity function. `products.stream().collect(Collectors.toMap(Product::getMonth, Function.identity()));` – Jan Krakora Jan 16 '17 at 10:05