2010-11-24 41 views
-1
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("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); 

} 
} 

输出::Hashtable的键获取错误的值

真:这意味着,如果两个物体varaibles是闯民宅指向同一对象equals()方法返回true Pillu

+1

干运行这个程序,我猜输出是人们所期望的。你的预期产出是多少? – 2010-11-24 03:08:01

回答

2

这是按预期工作。你在做什么是这样的:

person2 = person1; 

hs.put(person1, "Durvi"); 
hs.put(person2, "Pillu"); // since person1 == person2, 
          // this overwrites the previous key 

String personVal = (String)hs.get(person1); 

由于person2 == person1,最后调用等于

String personVal = (String)hs.get(person2); // person1 == person2 

作为一个侧面说明,你需要实现equals()hashCode()HashingTest类,一起来看看在这里:

0

你得到正确的行为在这种情况下,但...

你不能指望你已经做了正常的工作,因为你没有重写 hashcode()equals()

因为你是一个类,其实例被用作键在哈希容器(如 HashSetHashMapHashTable),您必须覆盖hashcode()equals()

http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf

0

默认情况下,来自Object的java的equals方法将比较引用。由于您将引用设置为相同,那么对于“等于”的调用将返回true。如果你想改变这种行为,那么你需要重写equals来检查字段值(以及任何你需要的)。

只是要小心,当你做到这一点 - 我建议什么乔希布洛赫了有效的Java说读了:

Item 8: Obey the general contract when overriding equals 
Item 9: Always override hashCode when you override equals 
0

看到,因为你设置PERSON1和PERSON2变量是相同的对象,

hs.put(person1, "Durvi"); 
    hs.put(person2, "Pillu"); 

相当于

hs.put(person1, "Durvi"); 
    hs.put(person1, "Pillu"); 
+0

谢谢......我刚刚检查了我的Hashtable的大小......它的3而不是4.现在我明白它被替换了! – NewQueries 2010-11-24 04:04:23

0

明白,在你的哈希表您已经使用对象引用作为键和String作为值。

两个PERSON1和PERSON2点在主方法相同的对象,其是通过

HashingTest("Durvi, 5, Female") 

hs.put(person1, "Durvi"); 
hs.put(person2, "Pillu"); 

上面的第一个放语句中创建的对象,创建具有“Durvi”的对象引用的条目,并分配价值“Durvi”。

由于哈希表中的键不能重复,第二行用“Pillu”替换上一行创建的值“Durvi”..

所以,当你执行get方法

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" 

我希望我已经说得很清楚.. 找回来,如果你需要进一步澄清..

不要没有注意到你有使用对象引用代替“key”。我希望你不会错误地做到这一点

+0

谢谢Ragavan – NewQueries 2010-11-24 04:07:10