import java.util.*;
public class HashingTest {
// instance variables
String name;
int age;
int hashCd;
String gender;
public HashingTest(String nm, int age, String gend)
{
name = nm;
this.age = age;
gender = gend;
}
public static void main(String[] args) {
HashingTest person1 = new HashingTest("Durvi",5,"Female");
HashingTest person2 = new HashingTest("Pillu",5,"Female");
HashingTest person3 = new HashingTest("Ninad",5,"Male");
HashingTest person4 = new HashingTest开发者_Go百科("Varun",5,"Male");
HashingTest person5 = new HashingTest("Sapna",5,"Female");
HashingTest person6 = new HashingTest("Priya",5,"Female");
//person2 and person1 are now referring to the same object
person2 = person1;
boolean truth = person1.equals(person2);
System.out.println(truth + " : Which means that if two object varaibles are refering the same object the equals() method returns true" );
Hashtable<HashingTest, String> hs = new Hashtable<HashingTest, String>();
hs.put(person1, "Durvi");
hs.put(person2, "Pillu");
hs.put(person3, "Ninad");
hs.put(person4, "Varun");
String personVal = (String)hs.get(person1);
System.out.println(personVal);
}
}
Output ::
true : Which means that if two object varaibles are refering the same object the equals() method returns true Pillu
This is working as expected. What you are doing is this:
person2 = person1;
hs.put(person1, "Durvi");
hs.put(person2, "Pillu"); // since person1 == person2,
// this overwrites the previous key
String personVal = (String)hs.get(person1);
Since person2 == person1
, the last call is equal to
String personVal = (String)hs.get(person2); // person1 == person2
As a side note, you need to implement equals()
and hashCode()
for your HashingTest
class, take a look here:
- http://www.ibm.com/developerworks/java/library/j-jtp05273.html
You are getting correct behavior in this case, but...
You can not expect what you have done to work correctly always because you have not overridden
hashcode()
and equals()
.
Since you are making a class whose instances are being used as keys in a hash container (like
HashSet
, HashMap
, and HashTable
), you must override hashcode()
and equals()
.
http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf
By default java's equals method from Object will compare references. Since you set the references to be the same then yes, the call to 'equals' will return true. If you wish to change that behavior then you need to override equals to check field values (and whatever else you need).
Just be careful when you do this - I'd suggest reading up on what Josh Bloch had to say in effective java:
Item 8: Obey the general contract when overriding equals
Item 9: Always override hashCode when you override equals
Seeing as you've set person1 and person2 variables to be the same object,
hs.put(person1, "Durvi");
hs.put(person2, "Pillu");
is equivalent to
hs.put(person1, "Durvi");
hs.put(person1, "Pillu");
Understand that in your Hashtable you have used the object reference as the key and the String as the value.
Both person1 and person2 points to the same object in the main method which is the object created by
HashingTest("Durvi, 5, Female")
hs.put(person1, "Durvi");
hs.put(person2, "Pillu");
The first put statement above, creates an entry with the object reference of "Durvi" and assigns the value "Durvi" to it.
As keys in Hashtables can't be duplicate, the second line replaces the value "Durvi" created by the previous line with "Pillu"..
So when you execute the get method
String personVal = (String)hs.get(person1);
//returns the value "Pillu" which the key person1 now refers to in the hash table.
System.out.println(personVal); //prints "Pillu"
I hope I have made it clear.. Get back if you need further clarifications..
Don't fail to notice that you have used the object reference in place of "key". I hope you didn't do it by mistake
精彩评论