Romeo Kienzler wrote:I think what Nina is talking about are the common mathematical set operations like intersection, difference, ...
But this works only for basic Java types (int, String, ...) and not for custom types.
So if you use custom types you must override the equals(Object o) - Method from the Object class.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Prime wrote:
Those Java statements won't compile; the bulk methods do not return a set but a boolean. The statements should be as followed (of course the Set implementation can change):
Rob Prime wrote:
Almost right. With HashSet and LinkedHashSet, you need to override both equals and hashCode. For TreeSet, you can go two ways:
- have your classes implement Comparable
- use a custom Comparator
Campbell Ritchie wrote:I am afraid it might not help; she was asking last November. Please look at this FAQ.
Romeo Kienzler wrote:
Rob Prime wrote:
Almost right. With HashSet and LinkedHashSet, you need to override both equals and hashCode. For TreeSet, you can go two ways:
- have your classes implement Comparable
- use a custom Comparator
With HashSet and LinkedHashSet you don't need necessarily overwrite hashCode. But data access will be a bit slower.
Since TreeSet is sorted, and this feature isn't needed for determining the cardinality of a Set, I consider the implementation of a comparator as unnecessary overhead.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Thank you. Our official policy is to discourage replies after more than about three weeks. And only too often, we find that is mistaken Sorry.Romeo Kienzler wrote:Hi Ritchie,
sometimes I'm searching for a particular solution to a problem and after I've found the solution I paste it to the forums where it hasn't been answered. Just for letting future searchers be more successful. But if this is undesired, I won't do it on JavaRanch in future.
Cheers,
Romeo
Rob Prime wrote:When you access an object, the set uses the hashCode of the object to determine the bucket. It then goes through the linked list using equals.
If you use the default hashCode implementation of Object, then it will most likely look in the wrong bucket. It there will not find an object that is equal, so nothing is found.