Filter collections with lambdaj

I have two classes:

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 you want to receive all orders with line items with specific content. At the moment, I am doing it like this:

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 this with lambdaj?

Thanks in advance.

+5
source share
1 answer

How about this: use org.hamcrest.Matchers.hasItem?

List<Order> outputOrders =
        filter(having(on(Order.class).getPositions(),
                      hasItem(having(on(Position.class).getContent(),
                                     equalTo("Something")))),
               orders);
+3
source

All Articles