开发者

Java Set collection - override equals method

开发者 https://www.devze.com 2023-03-09 17:40 出处:网络
Is there any way to override the the equals method used by a Set datatype? I wrote a custom equals method for a class called Fee. Now I have a LnkedList of Fee and I want to ensure that there are no d

Is there any way to override the the equals method used by a Set datatype? I wrote a custom equals method for a class called Fee. Now I have a LnkedList of Fee and I want to ensure that there are no duplicated entries. Thus I am considering using a Set insted of a LinkedList, but the criteria for deciding if two fees are equal resides in the overriden equals method in the Fee class.

If using a LinkedList, I will have to iterate over every list item and call the overriden equals method in the Fee class with the remaining entries as a parameter. Just reading this alone sounds like 开发者_开发百科too much processing and will add to computational complexity.

Can I use Set with an overridden equals method? Should I?


As Jeff Foster said:

The Set.equals() method is only used to compare two sets for equality.

You can use a Set to get rid of the duplicate entries, but beware: HashSet doesn't use the equals() methods of its containing objects to determine equality.

A HashSet carries an internal HashMap with <Integer(HashCode), Object> entries and uses equals() as well as the equals method of the HashCode to determine equality.

One way to solve the issue is to override hashCode() in the Class that you put in the Set, so that it represents your equals() criteria

For Example:

class Fee {
      String name;

  public boolean equals(Object o) {
      return (o instanceof Fee) && ((Fee)o.getName()).equals(this.getName());
  }

  public int hashCode() {
      return name.hashCode();
  }

}


You can and should use a Set to hold an object type with an overridden equals method, but you may need to override hashCode() too. Equal objects must have equal hash codes.

For example:

public Fee{

    public String fi;

    public String fo;

    public int hashCode(){

        return fi.hashCode() ^ fo.hashCode();
    }

    public boolean equals(Object obj){

        return fi.equals(obj.fi) && fo.equals(obj.fo);
    }
}

(With null checks as necessary, of course.)

Sets often use hashCode() to optimize performance, and will misbehave if your hashCode method is broken. For example, HashSet uses an internal HashMap.

If you check the source code of HashMap, you'll see it depends on both the hashCode() and the equals() methods of the elements to determine equality:

if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {

If the hash is not generated correctly, your equals method may never get called.

To make your set faster, you should generate distinct hash codes for objects that are not equal, wherever possible.


Set uses the equals method of the object added to the set. The JavaDoc states

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.

The Set.equals() method is only used to compare two sets for equality. It's never used as part of adding/remove items from the set.


One solution would be to use a TreeSet with a Comparator.

From the documentation:

TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal.

This approach would be much faster than using a LinkedList, but a bit slower than a HashSet (ln(n) vs n).

It's worth noting a one side effect of using TreeSet would be that your set is sorted.


There are PredicatedList or PredicatedSet in Apache Commons Collection

0

精彩评论

暂无评论...
验证码 换一张
取 消