Filtering collections with lambdaj
Asked Answered
T

1

6

I have two classes like that:

public class Order{
    private Integer id;
    private List<Position> positions;
    ...
}

public class Position{
    private Integer id;
    private String content;
    ...
}

Now, I have a list with orders and want to get all orders that have positions with a specific content. At the moment i do it like that:

List<Order> orders = ... ;

List<Order> outputOrders = ... ;
for(Order order : orders){
    if(select(order.getPositions(), having(on(Position.class).getContent(),equalTo("Something"))).size() != 0){
        outputOrders.add(order);
    }
}

Is there a better way to do that with lambdaj?

Thanks in advance.

Tamekia answered 18/3, 2013 at 8:43 Comment(0)
K
3

What about this one: use org.hamcrest.Matchers.hasItem?

List<Order> outputOrders =
        filter(having(on(Order.class).getPositions(),
                      hasItem(having(on(Position.class).getContent(),
                                     equalTo("Something")))),
               orders);
Kassie answered 18/3, 2013 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.