I have two sets in Java that compare Item
objects. Is there a method to compare the sets so that Item
's equals
method is invoked and not just compare references?
The equals() method of java. util. Set class is used to verify the equality of an Object with a Set and compare them. The method returns true if the size of both the sets are equal and both contain the same elements.
We often need to compare two sets to check whether they contain the same elements or not, and both sets should also have the same size. The Set interface provides the equals() method for verifying the equality of the given two sets. It returns either true or false based on the equality of both sets.
The equals() method of java. util. HashSet class is used verify the equality of an Object with a HashSet and compare them. The list returns true only if both HashSet contains same elements, irrespective of order.
Overview. The deepEquals method of the Objects class in Java is a static method used to check whether two given objects are deep equals. Two null values are always deeply equal. If both the objects passed are arrays, then Arrays.
Each child of AbstractSet
does that. See the docs
public boolean equals(Object o)
Compares the specified object with this set for equality. Returns true if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set. This ensures that the equals method works properly across different implementations of the Set interface. This implementation first checks if the specified object is this set; if so it returns true. Then, it checks if the specified object is a set whose size is identical to the size of this set; if not, it returns false. If so, it returns containsAll((Collection) o).
So in fact this relies on the contains
implementation (which is invoked by containsAll(..)
). For HashSet
(at least) this is what you are looking for.
This is the default behaviour, if it's not what you're seeing then check that you're overriding hashCode as well. See the following code for an example:
public static void main(String[] args) {
Set<Item> items1 = new HashSet<Item>();
items1.add(new Item("item 1"));
items1.add(new Item("item 2"));
Set<Item> items2 = new HashSet<Item>();
items2.add(new Item("item 1"));
items2.add(new Item("item 2"));
System.out.println(items1.equals(items2));
}
private static class Item {
private String id;
public Item(String id) {
this.id = id;
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public boolean equals(Object obj) {
return id.equals(((Item)obj).id);
}
}
This outputs:
true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With