user3185667
user3185667

Reputation: 51

Java, Sets particular HashSet TreeSet

Trying to save words from a .txt-file in a set. Never used sets before.

Set<Word> set = new HashSet<Word>();

When saving in the hashset I get size to 241. If I change the above to

Set<Word> set = new TreeSet<Word>();

and nothing else in the code, I get size to 231. Is there some kind of natural cause of this in the different sets?

Upvotes: 0

Views: 65

Answers (1)

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147154

You probably aren't making Comparable.compareTo consistent with Object.equals/hashCode. That or hashCode is not consistent with equals. Possibly not actually overridden hashCode - use @Override to make sure.

Upvotes: 6

Related Questions