vaibhavvc1092
vaibhavvc1092

Reputation: 3217

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. 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.

Currently I am creating two HashMaps representing two lists.

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?

Upvotes: 1

Views: 9750

Answers (4)

Alex Salauyou
Alex Salauyou

Reputation: 14338

I recommend to use Objects.equals() and Objects.hash() to implement equality check and hash code calculation. They are null-safe and easy to write and understand:

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.

After implementing these methods, you can safely use these objects in hash collections.

Upvotes: 2

Ankush soni
Ankush soni

Reputation: 1459

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:

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:

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
    }

Upvotes: 0

Casper
Casper

Reputation: 216

You could overwrite Equals and HashCode of CustomObj. Then use Contains() to test uniqueness.

Upvotes: 3

Amit Bhati
Amit Bhati

Reputation: 5649

Override the equals and hashcode method based on first five keys. Then you can just use equals method to compare the objects and use Collections bulk operations like retainAll etc.

Upvotes: 4

Related Questions