• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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.
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic