• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K & B: Collections Self-Test Question

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This had me a bit boggled as well, nice catch on that Keith.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic