Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

.equals()-Regarding collections

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the K&B 1.5 p. 526-27


Of Course,overriding the equals() for Car also allows the potential that more than one object representing a single unique car can exist, which might not be safe in your design. Fortunately the String and Wrapper classes work well as keys in hashtable-they override the equals() method.


I dont quite understand the above. What does the overriden equals() of String and wrapper class has so special about it that I couldnt do with my class car. If I override the equals() of car class how is that not as good as the overiden equals() of String.
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


First Check why we need to override hashcode and why we need to override equals method.
Check how hashcode and equals are interlinked with each other.
Check proper functions of map.

Then in above code comment and uncomment hashcode or equals method
and see result carefully.

If you will understand above code then you will be able to answer any question in exam regarding hashcode and equals method.
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic