The HashSet
class will use hashCode
and equals
to determine if there is a duplicate already in the set. The TreeSet
class will use the fact that it's Comparable
(or alternatively, a Comparator
) to order items and determine if there's a duplicate.
You have Bk
as Comparable<Bk>
, so TreeSet
will work properly with it. But, you still need to override hashCode
and equals
so HashSet
will work properly with it also.