| Author |
Help - Exam Watch about Collection -K&B
|
Alexsandra Carvalho
Ranch Hand
Joined: Jul 13, 2007
Posts: 75
|
|
Hello all, In page 543 K&B said:
When using HashSet or LinkedHashSet, the objects you add to them must override hashCode(). If they don�t override hashCode(), the default Object. hashCode() method will allow multiple objects that you might consider "meaningfully equal" to be added to your "no duplicates allowed" set.
But to avoid duplicates, I believe we must override equals() method, not hashCode, is not?? Thanks, Alexsandra [ December 11, 2007: Message edited by: Alexsandra Carvalho ]
|
 |
Alexsandra Carvalho
Ranch Hand
Joined: Jul 13, 2007
Posts: 75
|
|
|
But two diffente objects can have same hashcode...then, it (hashcode) does not say to us what objects are same or different.
|
 |
Lucas Lech
Greenhorn
Joined: Dec 10, 2007
Posts: 23
|
|
It's hashCode you need to override - it's how this data structure differentiates objects. try this @Override public int hashCode() { return (int) (Math.random()*100); } as hashCode and this public boolean equals(Object obj) { return true; } as equals and see what happens - you still can add "duplicates" if random returns different values.
|
 |
Lucas Lech
Greenhorn
Joined: Dec 10, 2007
Posts: 23
|
|
Originally posted by Alexsandra Carvalho: But two diffente objects can have same hashcode...then, it (hashcode) does not say to us what objects are same or different.
i think that's why *you* need to implement this method to fit your specific needs
|
 |
Lucas Lech
Greenhorn
Joined: Dec 10, 2007
Posts: 23
|
|
Also imagine time complexity of this structure if it used equals. Say you have 100K unique objects and you're adding another one. You would have to make 100K possibly time consuming comparisons before getting the answer: yes, I can add this object, it is unique [ O(n) ].
|
 |
Alexsandra Carvalho
Ranch Hand
Joined: Jul 13, 2007
Posts: 75
|
|
|
But hashcode is more related with performance and equals with individuality, is not?
|
 |
Lucas Lech
Greenhorn
Joined: Dec 10, 2007
Posts: 23
|
|
I believe your concern is due to the fact that implementation of Set interface is meant to use equals. However, HashSet is a bit different, look at this passage from the API docs: This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets. http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashSet.html Cheers, Lucas
|
 |
Luca Romanello
Greenhorn
Joined: May 30, 2002
Posts: 11
|
|
Hi all I don't know if I understood it right, but with HashSet, LinkedHashSet etc etc, testing whether an object is or isn't present in the Collection is a two-step process: firstly, the object's hashCode is calculated and searched amongst the "buckets" labelled by the already inserted objects' hashCodes, then, if it is found, the method equals() is run against all the object within the found bucket. HashCode doesn't say us which objects are the same, but different hashCodes SHOULD always tell us that their instances are different. Hope this helps Regards LR [ December 11, 2007: Message edited by: Luca Romanello ]
|
 |
 |
|
|
subject: Help - Exam Watch about Collection -K&B
|
|
|