java8从list集合中取出某一属性的值的集合

        List list = new ArrayList();
        Order o1 = new Order("1","MCS-2019-1123");
        list.add(o1 );
        Order o2= new Order("2","MCS-2019-1124");
        list.add(o2);
        Order o3= new Order("3","MCS-2019-1125");
        list.add(o3);
        List orderNoList=list.stream().map(Order::getOrderNo).collect(Collectors.toList());
        System.out.println("输出单号集合:"+orderNoList);
        List idList=list.stream().map(Order::getId()).collect(Collectors.toList());
        System.out.println(idList)

结果

输出第一个:["MCS-2019-1123", "MCS-2019-1124", "MCS-2019-1125"]
[1, 2, 3]

order类:

public class Order{
     String id;
     String orderNo;
     
     public Order(String id, String orderNo) {
        this.id = id;
        this.orderNo= orderNo;
    }
    public String getId() {
        return id;
    }
    public void setId(String order) {
        this.id= id;
    }
    public String getOrderNo() {
        return orderNo;
    }
    public void setOrderNo(String message) {
        this.orderNo= orderNo;
    }
    
}

 

你可能感兴趣的:(java基础)