在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ Java 8 lamada Stream: 如何獲取屬性的子屬性集合

Java 8 lamada Stream: 如何獲取屬性的子屬性集合

是這樣的,有一個(gè)訂單列表List<order>里面的order中包含若干的 goods ,現(xiàn)在想獲取訂單列表中,所有g(shù)oods的id,model結(jié)構(gòu)大致如下:

class order{
int id,
List<goods> goodsList;
}
class goods{
int id;
String name;
}

List<order> list....
這樣可以得到訂單的id列表,現(xiàn)在的問題是怎么獲取 list 所有 訂單下面商品集合的列表,
list.stream().map(c->c.getId()).collect(Collectors.toList())
。。。。。。
下面寫法貌似不對(duì)
list.stream().map(c->c.getGoodsList().stream().map(q->q.getId())).collect(Collectors.toList());

回答
編輯回答
擱淺

嵌套循環(huán)就好了

orders.stream.map(order->{
      return
      order.getGoodList().map(good->{
               return good.getId();
       }).collect(Collectors.toList());
}).collect (Collectors.toList());
2018年6月22日 21:00
編輯回答
心悲涼

試一試

list.stream()
    .map(order::getGoodsList)
    .flatmap(goods::getId)
    .map(x -> new Integer(x))
    .collect(Collectors.toList())

,要注意List里不能是int必須是Integer

2018年4月3日 02:30