Java – filter collections using lambdaj
•
Java
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 of orders and want to get all orders with specific content Now I do 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 use lambdaj?
Thank you in advance
Solution
How about that: use org hamcrest. Matchers. hasItem?
List<Order> outputOrders =
filter(having(on(Order.class).getPositions(),hasItem(having(on(Position.class).getContent(),equalTo("Something")))),orders);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
