簡體   English   中英

如何將 Array List 中某些字段的某些不同值與其他 List 進行比較並返回 java spring 中的差異?

[英]How to compare some different value of some field in Array List with other List and return the difference in java spring?

嗨,我想要實現的是在兩個列表中獲得一些屬性的不同值,這些列表具有一些相同的屬性,但並非全部相同。 我想獲得不同的值並將其作為已經改變的值返回。 這是我所擁有的:

我有第一個 DTO,它是從前端 ShipmentAddressGroupingDtDto.java 提交的,看起來像這樣:

@Data
public class ShipmentAddressGroupingDtDto {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long id;

    public String address;

    public Boolean isActive;

    public Long partnerShipmentId;
    
}

這是我的 DTO,我得到了另一個 API,PartnerShipmentDto.java:

@Data
public class PartnerShipmentDto {

    public Long id;

    public String partnerShipmentCode;

    public String address;

    public String phoneNumber;

    public String region;

    public String subPartnerName;

    public String subPartnerAddress;

    public Integer olt;

    public String typeShipment;

    public Boolean allowBigVehicle;

    public Boolean isActive;
    
}

我想使用兩個屬性比較這兩個 object,第一個字段 isActive 和第二個地址,如果這些字段不同,則在遠程(其他 API)端更改,我想通知用戶該值已更改為新值,並且將來我想比較另一個同名屬性,而不僅僅是這兩個屬性(isActive 和 address)。

我需要比較兩個列表,第一個shippingAddressGroupingDto 和第二個 partnerShipmentDto

注意:在 PartnerShipmentDto.getId 中是指 ShipmentAddressGroupingDtDto.getPartnerShipmentId 作為推薦

這是我在服務中所做的。java:

//here is how i got the data from front end 
        ShipmentAddressGroupingDto shipmentAddressGroupingDto = request.getObject();
        List<ShipmentAddressGroupingDtDto> shipmentAddressGroupingDtDto = shipmentAddressGroupingDto.getShipmentAddressGroupingDtDto();

// here is how i got the data from remote API
        List<PartnerShipmentDto> partnerShipmentDto = webClient.post().uri(url).body(Mono.just(partnerShipmentIds),
            PartnerShipmentDto.class).retrieve().bodyToMono(new ParameterizedTypeReference<List<PartnerShipmentDto>>() {}).block();


    for(int i=0; i < shipmentAddressGroupingDtDto.size(); i++) {
        ShipmentAddressGroupingDtDto s = shipmentAddressGroupingDtDto.get(i);
        for(int a=0; a < partnerShipmentDto.size(); a++) {
            if(s.getPartnerShipmentId().equals(partnerShipmentDto.get(a).getId())){
                Boolean isActiveResult = s.getIsActive().equals(partnerShipmentDto.get(a).getIsActive());
                Boolean addressResult = s.getAddress().equals(partnerShipmentDto.get(a).getAddress());

                if(!isActiveResult){
                    throw new ResourceAlreadyChange("Active already change");
                } else if(!addressResult) {
                    throw new ResourceAlreadyChange("Address already change");
                }
            }
        }
    }

我如何在沒有很多 for 循環並為每個字段添加 if 的情況下實現此結果? java 是否有一些功能可能使用 lambda 表達式或其他東西來實現這一點? 也許使用一維或二維循環是可以的,但不使用基於屬性的 if 條件,java 是否可以找到一些同名屬性並進行比較並得到它們之間有差異的值(左側或右側都可以)? 所以我不必為我必須比較的每個屬性添加 if 條件,因為它們已經具有相同的屬性名稱。 請幫我舉個例子

我的 service.java 夠清楚嗎? 如果我不是很清楚,我會改進我的問題

我的建議是使用 Java 流使其更具可讀性。 否則想不到太多改進。

希望這仍然有幫助:)

shipmentAddressGroupingDtDto.stream().forEach(shipmentAdressGroupingDto -> {
  partnerShipmentDto.stream().forEach(partnerShipmentDto -> {
      if(partnerShipmentDto.getId().equals(shipmentAdressGroupingDto.getId())){
          Boolean isActiveResult = s.getIsActive().equals(partnerShipmentDto.get(a).getIsActive());
          Boolean addressResult = s.getAddress().equals(partnerShipmentDto.get(a).getAddress());

          if(!isActiveResult){
             throw new ResourceAlreadyChange("Active already change");
          }else if(!addressResult){
             throw new ResourceAlreadyChange("Address already change");
          }
      }
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM