如何用Lambda表达式获取List中的一个元素集合

2020-04-18  本文已影响0人  ACtong
{
  "goods": [
    {
        "id": 12345,
        "number": 10,
    },
    {
        ...
    }
}
List<Long> goodsId = request.getGoods()
                .stream()
                .map(AddToShoppingCartItem::getId)
                .collect(toList());
public static class AddToShoppingCartRequest {
        List<AddToShoppingCartItem> goods;

        public List<AddToShoppingCartItem> getGoods() {
            return goods;
        }

        public void setGoods(List<AddToShoppingCartItem> goods) {
            this.goods = goods;
        }
    }

public static class AddToShoppingCartItem {
        long id;
        int number;

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public int getNumber() {
            return number;
        }

        public void setNumber(int number) {
            this.number = number;
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读