2010-11-24 31 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