Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

doubt in Ch7 of K & B book

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I have a doubt in question number 7 of SELF TEST of Chapter 7 (Generics and Collections) of K&B book-
Here is the code-



Options are -




Correct answer is --
3 C and D are correct. If hashCode() is not overridden then every entry will go into its own
bucket, and the overridden equals() method will have no effect on determining equivalency.
If hashCode() is overridden, then the overridden equals() method will view t1 and t2 as duplicates.
-------------------------------------------------------------------

Now, my questions are
1. Will equals method not at all run if there is no hashCode method in our class?
2. What is the sequence of execution of equals and hashCode methods. I suppose hashCode method runs first..am I right?
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi prajal,
there are n buckets say 1, 2, 3, 4 ...n

whenever you are trying to insert value in hash table , or hash map first it will get the hashcode using the getHashCode method on that object whcih you are trying to insert.

Then using that bucket number it will find is there any other object which is equals to the current object which you are trying to insert using equals method. If is finds then it will replace the existing object with new object. other wise it will insert new object in that bucket.

now review the question you posted and confirm that answers given by your self.

if you have still doubt you are free to ask.

thanks
rami.
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic