File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help - Exam Watch about Collection -K&B

 
Alexsandra Carvalho
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But hashcode is more related with performance and equals with individuality, is not?
 
Lucas Lech
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic