• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

hashtable working & overriding equals()

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,
thanx for the tremendous response for clearing my earlier doubt in string-pool.expecting similar responses from u all,i m posting yet another question and its regarding hashtable working.

we know that hashtable stores values as key/value pairs,where both key and value should b an object.

considering this code,

class car
{
int vehnum;
int yearofmake;

public static void main(String args[])
{
car ob1=new car();
car ob2=new car();

Hashtable hs=new Hashtable();

hs.put(ob1,"Raja");
hs.put(ob2,"Alps");
hs.put(ob1,"Rahi");

String s=(String)hs.get(ob1);
System.out.println(s);
}//end of main

}// end of class car

now the o/p obtained is Rahi.i understand why is it so.as i have reffered the key ob1 for Rahi at last.

i want to know how the hashtable retreives the data,and how can i override equals() method,so that key/value pair is unique.

please give thw way as how i should override the equals() method to make my code more efficient.
 
author and iconoclast
Posts: 24208
47
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't post duplicate threads. It just wastes people's time if they answer a question that's already been answered elsewhere. I'm closing this one.
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic