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

        List<Order> list = new ArrayList<User>();
        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<String> orderNoList=list.stream().map(Order::getOrderNo).collect(Collectors.toList());
        System.out.println("输出单号集合:"+orderNoList);
        List<String> 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;
    }
    
}

 

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页