Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K & B: Collections Self-Test Question

 
Venkat Sidh
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Here is the question on Page 630 & Question #12 ,



The code doesn't compile and it is fine, but if I change the code to,



And changing Person's class to



then the answer is A,B,C,E but I thought it should be A,B,C,D since Person overrides equals() and hashcode() and mutually comparable then how come duplicates are added to Set. Am I missing something here? Thanks.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I admit I puzzled over this for awhile until I spotted the error.

Note that in your definition of Person, you have the method hashcode().

However, the method you need to override from Object is hashCode().
 
Venkat Sidh
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops...good caught. I never realized it's a typo error.

[B][/B]ofcourse

Anyway its a good lesson to prepare for test.
 
Matthew Gerald
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This had me a bit boggled as well, nice catch on that Keith.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,


annotations are not on the exam. But use it!

@Override

above your methods, and the compiler would shout out loud.

eg



will not compile, because one method is called em-two-el and the other m-twentyone.


Yours,
Bu.
[ October 28, 2006: Message edited by: Burkhard Hassel ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic