简体   繁体   English

比较Java中两个对象列表的最佳方法

[英]Best way to compare Two Lists of Objects in Java

I have two lists of Objects. 我有两个对象列表。

public class CustomObj {
    Long key1;
    Integer key2;
    Integer key3;
    Integer key4;
    Integer key5;
    BigDecimal value1
    BigDecimal value2
    BigDecimal value3
    BigDecimal value4
    BigDecimal value5; }

public class CustomKey {
    Long key1;
    Integer key2;
    Integer key3;
    Integer key4;
    Integer key5; }

Object does not have a unique identifier, but 5 fields key1 to key5 from object makes a unique key for object. 对象没有唯一标识符,但是对象的5个字段key1到key5构成了对象的唯一键。 I have to compare such two lists of objects. 我必须比较这两个对象列表。 I have to compare values inside object only if unique key consisting of key1-key5 fields are same in both the objects. 仅当两个对象中由key1-key5字段组成的唯一键相同时,才必须比较对象内部的值。

Currently I am creating two HashMaps representing two lists. 目前,我正在创建代表两个列表的两个HashMap。

Map<CustomKey, CustomObj>

Then, I iterate over first hashmap, for each key I check if that key exists in other hashmap and if it does, I get the object from other hashmap and then compare these two objects. 然后,我遍历第一个哈希图,对于每个键,我检查该键是否在其他哈希图中存在,如果存在,则从其他哈希图中获取对象,然后比较这两个对象。

Is there any better way of doing it? 有什么更好的方法吗?

Override the equals and hashcode method based on first five keys. 根据前五个键覆盖equals和hashcode方法。 Then you can just use equals method to compare the objects and use Collections bulk operations like retainAll etc. 然后,您可以使用equals方法比较对象并使用Collections批量操作(如keepAll等)。

You could overwrite Equals and HashCode of CustomObj. 您可以覆盖CustomObj的Equals和HashCode。 Then use Contains() to test uniqueness. 然后使用Contains()测试唯一性。

I recommend to use Objects.equals() and Objects.hash() to implement equality check and hash code calculation. 我建议使用Objects.equals()Objects.hash()来实现相等性检查和哈希码计算。 They are null-safe and easy to write and understand: 它们是null安全的,易于编写和理解:

public class CustomKey {
    Long key1;
    Integer key2;
    Integer key3;
    Integer key4;
    Integer key5; 

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof CustomKey)) return false;
        CustomKey k = (CustomKey) o;
        return Objects.equals(key1, k.key1)
            && Objects.equals(key2, k.key2)
            && Objects.equals(key3, k.key3)
            && Objects.equals(key4, k.key4)
            && Objects.equals(key5, k.key5);
    }    

    @Override
    public int hashCode() {
        return Objects.hash(key1, key2, key3, key4, key5);
    }
}

Similar with CustomObj . CustomObj相似。

After implementing these methods, you can safely use these objects in hash collections. 在实现这些方法之后,您可以安全地在哈希集合中使用这些对象。

Hope this will help you out, here is you class: 希望这会对您有所帮助,这是您的课程:

  1. Class CustomObj override the Object's equal method and in the implementation I am compairing attributes of Class CustomObj with CustomKey CustomObj类重写Object的equal方法,在实现中,我使用CustomKey对Class CustomObj属性进行CustomObj

CustomObj: CustomObj:

public class CustomObj {
    Long key1;
    Integer key2;
    Integer key3;
    Integer key4;
    Integer key5;
    BigDecimal value1;
    BigDecimal value2;
    BigDecimal value3;
    BigDecimal value4;
    BigDecimal value5;

    public CustomObj(Long k1, Integer k2, Integer k3, Integer k4, Integer k5){
        this.key1 = k1;
        this.key2 = k2;
        this.key3 = k3;
        this.key4 = k4;
        this.key5 = k5;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key1 == null) ? 0 : key1.hashCode());
        result = prime * result + ((key2 == null) ? 0 : key2.hashCode());
        result = prime * result + ((key3 == null) ? 0 : key3.hashCode());
        result = prime * result + ((key4 == null) ? 0 : key4.hashCode());
        result = prime * result + ((key5 == null) ? 0 : key5.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        CustomKey other = (CustomKey) obj;
        if (key1 == null) {
            if (other.key1 != null)
                return false;
        } else if (!key1.equals(other.key1))
            return false;
        if (key2 == null) {
            if (other.key2 != null)
                return false;
        } else if (!key2.equals(other.key2))
            return false;
        if (key3 == null) {
            if (other.key3 != null)
                return false;
        } else if (!key3.equals(other.key3))
            return false;
        if (key4 == null) {
            if (other.key4 != null)
                return false;
        } else if (!key4.equals(other.key4))
            return false;
        if (key5 == null) {
            if (other.key5 != null)
                return false;
        } else if (!key5.equals(other.key5))
            return false;
        return true;
    }

}

CustomKey: CustomKey:

public class CustomKey {
    Long key1;
    Integer key2;
    Integer key3;
    Integer key4;
    Integer key5;

    public CustomKey(Long k1, Integer k2, Integer k3, Integer k4, Integer k5){
        this.key1 = k1;
        this.key2 = k2;
        this.key3 = k3;
        this.key4 = k4;
        this.key5 = k5;
    }
}

To Test it:



public static void main(String[] args) {
        CustomObj customObj = new CustomObj(123L, 11, 34, 45, 99);
        CustomKey customKey = new CustomKey(123L, 11, 34, 45, 99);
        CustomKey customKey2 = new CustomKey(124L, 12, 34, 45, 99);

        System.out.println(customObj.equals(customKey));// This will return true since the key is same
        System.out.println(customObj.equals(customKey2));// This will return false since the key is same
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM