Java HashSet is allowing dupes; problem with compa

2019-04-21 08:32发布

I've got a class, "Accumulator", that implements the Comparable compareTo method, and I'm trying to put these objects into a HashSet.

When I add() to the HashSet, I don't see any activity in my compareTo method in the debugger, regardless of where I set my breakpoints. Additionally, when I'm done with the add()s, I see several duplicates within the Set.

What am I screwing up, here; why is it not Comparing, and therefore, allowing the dupes?

Thanks,
IVR Avenger

8条回答
We Are One
2楼-- · 2019-04-21 09:03

When hashCode return different values for 2 objects, then equal is not used. Btw, compareTo has nothing to do with hashing collections :) but sorted collections

查看更多
相关推荐>>
3楼-- · 2019-04-21 09:04

HashSet uses hashCode and equals. TreeSet uses the Comparable interface. Note: if you decide to override either hashcode or equals, you should always override the other.

查看更多
Juvenile、少年°
4楼-- · 2019-04-21 09:06

Your objects are Comparable, and probably you've implemented equals() too, but HashSets deal with object hashes, and odds are you haven't implemented hashCode() (or your implementation of hashCode() doesn't return the same hash for two objects that are (a.equals(b) == true).

查看更多
唯我独甜
5楼-- · 2019-04-21 09:08

What am I screwing up, here?

HashSet is based on hashCode(), not on compareTo(). You may be confusing it with TreeSet. In both cases, be sure to also implement equals() in a manner that is consistent with the other method.

查看更多
\"骚年 ilove
6楼-- · 2019-04-21 09:18

HashSet uses the hashCode() and equals() methods to prevent duplicates from being added. First, it gets the hash code of the object you want to add. Then, it finds the corresponding bucket for that hash code and iterates through each object in that bucket, using the equals() method to see if any identical objects already exist in the set.

Your debugger is not breaking on compareTo() because it is never used with HashSet!

The rules are:

  1. If two objects are equal, then their hash codes must be equal.

  2. But if two objects' hash codes are equal, then this doesn't mean the objects are equal! It could be that the two objects just happen to have the same hash.

查看更多
叼着烟拽天下
7楼-- · 2019-04-21 09:18

One thing which people tends to ignore which result in a huge mistake. While defining equals method always take the parameter as object class and then conver the object to your desired class. For eg

   public bolean equals(Object aSong){
     if(!(aSoneg instanceof Song)){
       return false;
     }
     Song s=(Song) aSong;
     return getTitle().equals(s.getTitle());
   }

If u pass write Song aSong instead of Object aSong your equals method will never get called.

Hope this helps

查看更多
登录 后发表回答