Search code examples
javalistforeachgetdto

How to get value from list that is in another list which in another list


I have a Dto class with variables which one of them is another class. and inside another class there is a variable List with another class.

public class GetList {
    private String id;
    private ResultGetList result;
}


public class ResultGetList {
    @JsonProperty("Customer")
    private List<CustomerGetList> customer;
}

public class CustomerGetList {
    @JsonProperty("customerId")
    private String customerId;
    @JsonProperty("bankId")
    private String bankId;
    @JsonProperty("cardholderName")
    private String cardHolderName;
    @JsonProperty("Card")
    private List<CardGetList> Card;
}

I open new Dto class for get necessary variables in above classes.

public class NewConDto {

    private String id; 
    private String pan;
    private String mfo;
    private String fullName;
}

I need set NewConDto values from above Classes.

 NewConDto newConDto= new NewConDto();
newConDto.setid(GetList.getResult().getCustomer()....)

i don't get after getCustomer because, next value is List. I need get value from List value which a another Dto class.

Generally speaking, I need something like this:

GetList.getResult().getCustomer().foreach(.....getcustomerId());
GetList.getResult().getCustomer().foreach(.....getpan());
GetList.getResult().getCustomer().foreach(.....getmfo());

....

How to make for each, to take value from class. Or maybe there is another solution?


Solution

  • Assuming

    • You have the correct getters and setters in the class, and you are not showing them here for keeping code short)humoGetList -> instance of GetList
    • humoGetList.getResult() -> results in instance of ResultGetList
    • humoGetList.getResult().getCustomer() -> -> results in instance of List< CustomerGetList>

    You need to create a new List newConDtoList and fill it with objects. Here are few options:

    public class MyProgram{
      public static void main(String[] args) {
        GetList getList = testData();
    
        // Option 1 : Use For Each Loop
    
        List<NewConDto> newConDtoList1 = new ArrayList<>();
        for (CustomerGetList customer : testData().getResult().getCustomer()){
          NewConDto newConDto =  convert(customer);
          newConDtoList1.add(newConDto);
        }
    
        // Option 2: Use for each
        List<NewConDto> newConDtoList2 = new ArrayList<>();
        getList.getResult().getCustomer().forEach(MyProgram::convert);
    
        // Option 3: Use stream
    
        List<NewConDto> newConDtoList3 = getList.getResult().getCustomer().stream()
          .map(MyProgram::convert)
          .collect(Collectors.toList());
    
    
      }
    
      private static NewConDto convert(CustomerGetList customer){
        NewConDto newConDto = new NewConDto();
        newConDto.setFullName(customer.getCardHolderName());
        newConDto.setId(customer.getBankId());
        //...
        return newConDto;
      }
      private static GetList testData(){
      ResultGetList resultGetList = new ResultGetList();
    
        CustomerGetList customerGetList1 = new CustomerGetList();
        customerGetList1.setBankId("bankId1");
        customerGetList1.setCardHolderName("cardHolderName1");
        customerGetList1.setCustomerId("customerId1");
        CardGetList cardList1 = new CardGetList();
        customerGetList1.setCard(Arrays.asList(cardList1));
    
        CustomerGetList customerGetList2 = new CustomerGetList();
        customerGetList2.setBankId("bankId2");
        customerGetList2.setCardHolderName("cardHolderName2");
        customerGetList2.setCustomerId("customerId2");
        CardGetList cardList2 = new CardGetList();
        customerGetList2.setCard(Arrays.asList(cardList2));
    
        GetList getList = new GetList();
        getList.setResult(resultGetList);
    
        return getList;
      }
    }