简体   繁体   中英

How to create ArrayList of an object present within Arraylist?

I have following use case

 class Agree{

 }

 class Agreebody{
    Agree agree;
 }

 class AgreeFull{
   List<AgreeBody> agreebody;
 }

The requirement is to create a List of all Agrees present inside AgreeBody List.

 List<Agree> l = new List<Agree>
 agreeFull.getAgreeBody.stream(). ? //Need to get Agree from Each agreebody and make list of agrees

Shall i call .forEach and do it manually or other better way?

You can use map

List<Agree> l = agreeFull.getAgreebody().stream()
        .map(AgreeBody::getAgree)
        .collect(Collectors.toList());

You need also to focus on some part of your code, for example using getter and setters, significant name for the attributes and class..

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