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

huangapple go评论61阅读模式
英文:

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

问题

你想要比较两个列表中的对象,并找出其中某些属性的不同值,然后将其返回为已更改的值。你也希望以后能够比较更多相同名称的属性。你可以尝试使用Java Stream API 来简化这个过程,减少嵌套的for循环和if条件。以下是一个示例代码片段:

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

// 将PartnerShipmentDto列表转换为Map,以id作为键
Map<Long, PartnerShipmentDto> partnerShipmentDtoMap = partnerShipmentDto.stream()
        .collect(Collectors.toMap(PartnerShipmentDto::getId, Function.identity()));

// 比较并查找不同的属性
for (ShipmentAddressGroupingDtDto s : shipmentAddressGroupingDtDto) {
    PartnerShipmentDto partnerShipment = partnerShipmentDtoMap.get(s.getPartnerShipmentId());

    if (partnerShipment != null) {
        // 使用Java Stream的allMatch方法来检查所有属性是否相同
        boolean isDifferent = !List.of(
                s.getIsActive().equals(partnerShipment.getIsActive()),
                s.getAddress().equals(partnerShipment.getAddress())
                // 添加其他要比较的属性
        ).stream().allMatch(Boolean::booleanValue);

        if (isDifferent) {
            throw new ResourceAlreadyChange("某些属性已更改");
        }
    }
}

这个示例代码使用了Java Stream API,首先将partnerShipmentDto列表转换为一个以id作为键的Map,然后在循环中比较两个对象的属性值。你可以继续添加其他要比较的属性,并扩展这个示例以适应更多的属性比较需求。

希望这能帮助你简化代码并更轻松地比较属性。如果有其他问题或需要进一步的帮助,请告诉我。

英文:

Hi what i trying to achieve is to get a different value of some properties in two list which have some same properties but not all is same. i want to get the different value and return it as a value that already change. here is what i have :

i have first DTO, which is submited from frontend ShipmentAddressGroupingDtDto.java that looked like this :

@Data
public class ShipmentAddressGroupingDtDto {

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

    public String address;

    public Boolean isActive;

    public Long partnerShipmentId;
    
}

and here is my DTO that i got front another 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;
    
}

i want compare these two object using two properties, first field isActive and second address, if these field is different, which is changed on the remote (Other API) side, i want notice the user that the value has changed into new value, and in the future i want compare another same name properties not just these two properties (isActive and address).

i need to compare two list, first shipmentAddressGroupingDto and second partnerShipmentDto.

notes : in PartnerShipmentDto .getId is refer to ShipmentAddressGroupingDtDto .getPartnerShipmentId as the referral

Here is what i do, in my service.java :

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

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


    for(int i=0; i &lt; shipmentAddressGroupingDtDto.size(); i++) {
        ShipmentAddressGroupingDtDto s = shipmentAddressGroupingDtDto.get(i);
        for(int a=0; a &lt; 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(&quot;Active already change&quot;);
                } else if(!addressResult) {
                    throw new ResourceAlreadyChange(&quot;Address already change&quot;);
                }
            }
        }
    }

How can i achieve this result without to many for loop and add if for each field? is java have some feature maybe using lambda expression or something else to achieve this? maybe using one or two dimension for loop is okay but not using if condition based in the properties, can java find some same name properties and compare it and get the value that have difference(on left or right side is okay) between them? so i don't have to add if condition for every properties that i have to compare because they already have same properties name. please help me with example

is my service.java clear enough? i will improve my question if i am not really clear

答案1

得分: 1

我的建议是使用Java流(Java streams)来使代码更可读。否则,我没有想到太多可以改进的地方。

希望这仍然有所帮助 How to compare some different value of some field in Array List with other List and return the difference in java spring?

英文:

My recommendation would be to use Java streams to make it a bit more readable. Otherwise there isn't much improvement that comes to mind.

Hope this still helps tho How to compare some different value of some field in Array List with other List and return the difference in java spring?

shipmentAddressGroupingDtDto.stream().forEach(shipmentAdressGroupingDto -&gt; {
  partnerShipmentDto.stream().forEach(partnerShipmentDto -&gt; {
      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(&quot;Active already change&quot;);
          }else if(!addressResult){
             throw new ResourceAlreadyChange(&quot;Address already change&quot;);
          }
      }
   }
}

</details>



huangapple
  • 本文由 发表于 2020年8月14日 11:07:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63405946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定