如何从另一个列表中的另一个列表中的列表中获取值

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

我有一个带有变量的 Dto class,其中一个是另一个 class。在另一个 class 中有一个变量 List 和另一个 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;
}

我打开新的 Dto class 以获得上面 classes 中的必要变量。

public class NewConDto {

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

我需要从上面设置 NewConDto 值 类。

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

我没有在 getCustomer 之后获取,因为下一个值是 List。 我需要从另一个 Dto class.

的列表值中获取值

一般来说,我需要这样的东西:

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

.....

如何为每一个做出贡献,从class中获取价值。或者也许还有其他解决方案?

假设

  • 您在 class 中有正确的 getter 和 setter,并且您没有在此处显示它们以保持代码简短)humoGetList -> GetList 的实例
  • humoGetList.getResult() -> ResultGetList
  • 实例的结果
  • humoGetList.getResult().getCustomer() -> -> 结果 List< CustomerGetList>

您需要创建一个新列表 newConDtoList 并用对象填充它。这里有几个选项:

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;
  }
}