aspose file tools*
The moose likes Java in General and the fly likes hashtable working & overriding equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "hashtable working & overriding equals()" Watch "hashtable working & overriding equals()" New topic
Author

hashtable working & overriding equals()

rajaraman navaneethan
Ranch Hand

Joined: Feb 26, 2005
Posts: 86
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: hashtable working & overriding equals()