import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
@org.junit.Test
public void test1(){
List<Order> orderList = new ArrayList<>();
Order order1 = new Order("1","1","1",new BigDecimal("8.78"),"2 apples");
Order order2 = new Order("2","1","2",new BigDecimal("2.78"),"2 potatos");
Order order3 = new Order("3","2","1",new BigDecimal("12.78"),"4 apples");
orderList.add(order1);
orderList.add(order2);
orderList.add(order3);
// 过滤userId=1 amt>5的数据
List<Order> list = orderList.stream()
.filter(e -> e.getAmt().compareTo(new BigDecimal("5")) > 0
&& "1".equals(e.getUserId()))
.collect(Collectors.toList());
System.out.println(list.stream().map(Order::getOrderId).collect(Collectors.joining(",")));
}
private class Order{
private String orderId;
private String userId;
private String productId;
private BigDecimal amt;
private String desc;
public Order(String orderId, String userId, String productId, BigDecimal amt, String desc) {
this.orderId = orderId;
this.userId = userId;
this.productId = productId;
this.amt = amt;
this.desc = desc;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public BigDecimal getAmt() {
return amt;
}
public void setAmt(BigDecimal amt) {
this.amt = amt;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
}
标签:String,list,userId,Order,过滤,public,amt,BigDecimal
From: https://www.cnblogs.com/mmcode/p/18458966