Jan Krakora
Jan Krakora

Reputation: 2610

Java stream alternative to LambdaJ index

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.

Upvotes: 4

Views: 727

Answers (1)

Eugene
Eugene

Reputation: 120848

 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

Upvotes: 4

Related Questions