Search code examples
javajava-streamlambdaj

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.


Solution

  •  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