[英]How to process element of list to another list in single line in java 8
There is a list of View2
object, I want to add the Id
of View2
object in List<Integer> Ids
.有列表View2
的对象,我想补充的Id
的View2
对象List<Integer> Ids
。 I want to process the above operation in a single line of code.我想在一行代码中处理上述操作。
class View1{
List<Integer> Ids;
}
class View2{
Integer Id;
}
List<View2> views2 = getViews()
Want to something like following.想要像下面这样的东西。
view1.setIds(view2.forEach(s -> s.getId()));
尝试这个
view1.setIds(view2.stream().map(View2::getId).collect(Collectors.toList()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.