【问题标题】:compare two objects of same class which contain list of objects比较包含对象列表的同一类的两个对象
【发布时间】:2018-02-09 11:18:20
【问题描述】:

我有两个相同类的对象(包含对象列表)。我需要找出两者是否相同。

考虑下面的例子:

class Device {

    String deviceName;
    String devLocation;
    String devType;
    String devID;

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public String getDevLocation() {
        return devLocation;
    }

    public void setDevLocation(String devLocation) {
        this.devLocation = devLocation;
    }

    public String getDevType() {
        return devType;
    }

    public void setDevType(String devType) {
        this.devType = devType;
    }

    public String getDevId() {
        return devID;
    }

    public void setDevId(String devId) {
        this.devID = devId;
    }

}


class DevList {

        List<Device> deviceList;

        public List<Device> getDevices() {
            return deviceList;
        }

        public void setDevices(List<Device> deviceList) {
            this.deviceList = deviceList;
        }
    }

需要比较 DevList 类的两个对象。

将在每个固定时间间隔获得一个新的 DevList 对象。

每次我需要用前一个对象验证当前对象并更新数据库,如果有任何差异,则忽略。

当前和先前对象中的列表(deviceList)可能顺序不同。

例如:

考虑以下两个 Json 格式的对象(请忽略 json 格式错误)。

对象 1:

{  
   "devList":[  
      {  
         "deviceName":"ABC",
         "devLocation":"India",
         "devType":"Router",
         "devID":"1111"
      },
      {  
         "deviceName":"XYZ",
         "devLocation":"India",
         "devType":"Router",
         "devID":"2222"
      }
   ]
}

对象 2:

{  
   "devList":[  
      {  
         "deviceName":"XYZ",
         "devLocation":"India",
         "devType":"Router",
         "devID":"2222"
      },
      {  
         "deviceName":"ABC",
         "devLocation":"India",
         "devType":"Router",
         "devID":"1111"
      }
   ]
}

我们可以通过检查 devID 来迭代两个对象中的列表来做到这一点。但复杂度是 M*N。

还有其他方法吗?

【问题讨论】:

  • 如果您希望两个设备列表仅在它们的顺序不同时被视为相等,那么您需要使用Set&lt;Device&gt; 而不是List&lt;Device&gt;。请注意,您还需要在 Device 类中添加正确的 equals(Object) 和 hashCode() 方法。

标签: java json compare


【解决方案1】:

您可以覆盖Device 中的equals 以比较每个Device 对象。

类似的东西

class Device {

 ...
 ...
 //your getter and setter

    public boolean equals (Object obj)
    {
      if (Device.class != obj.getClass()) {
        return false;
      }
      if (obj instanceof Device) {
            return (deviceName.equals(((Device) obj).deviceName) &&
                    devLocation.equals(((Device) obj).devLocation) &&
                    devType.equals(((Device) obj).devType) &&
                    devID.equals(((Device) obj).devID));
      }
      return false;
    }

}

现在,调用 removeAll,这将从 List1 中删除相同的对象列表

List1.equals(List2);

因此,如果两个列表具有相同的对象列表,则 List1 将为空。

【讨论】:

    【解决方案2】:

    第 1 步:首先在设备类中重写 equal 方法 第 2 步:覆盖 DevList 类中的 equal() 具有以下逻辑 1.使用一个Set集合 2.从第一个对象的列表中添加对象 3. 检查 set 的 size() 3.从第二个对象的列表中添加对象 5. 每次从第二个列表添加时检查 set 的 size() 6.如果超过一个列表的大小不相等(As Set 不会保留重复值)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多