简体   繁体   中英

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 . 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()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM