Déb*_*ora 5 java collections lambda java-8
有一个List<MyObject>和它的对象需要创建将添加到具有不同元素的另一个List的对象:List<OtherObject>.     
这就是我的表现,
List<MyObject> myList = returnsList();
List<OtherObj> emptyList = new ArrayList();
for(MyObject obj: myList) {   
    OtherObj oo = new OtherObj();
    oo.setUserName(obj.getName());
    oo.setUserAge(obj.getMaxAge());   
    emptyList.add(oo);  
}
我正在寻找一个lamdba完全相同的表达方式.
如果定义构造函数OtherObj(String name, Integer maxAge),则可以使用java8样式:
myList.stream()
    .map(obj -> new OtherObj(obj.getName(), obj.getMaxAge()))
    .collect(Collectors.toList());
这将在列表中的所有对象映射myList到OtherObj它收集到新的List含有这些对象.
| 归档时间: | 
 | 
| 查看次数: | 18681 次 | 
| 最近记录: |