[英]Java 8 Lambda Map<Integer,List<Object> for each create a new object and add this new objet in a list
每個Java 8 Lambda Map都會創建一個新對象並將此新對象添加到列表中
這是我的第一步
Map<Integer, List<Obj>> objGroupBy = list.stream().collect(Collectors.groupingBy(Obj::getSomething));
List<Obj2> lst2= new ArrayList<Obj2>();
for (Entry<Integer, List<Obj>> entry : objGroupBy .entrySet())
{
lst2.add(new Obj2(entry.getKey().intValue(), entry.getValue()));
}
我想知道是否可以用lambda來做謝謝!
等效於您的代碼:
List<Obj2> lst2 = list.stream()
.map(o -> new Obj2(o.getSomething(), o))
.collect(Collectors.toList());
現在,如果您真的想以相同的方式開始:
List<Obj2> lst2 = list.stream()
.collect(Collectors.groupingBy(Obj::getSomething))
.entrySet()
.stream()
.map(e -> new Obj2(e.getKey(), e.getValue()))
.collect(Collectors.toList());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.