哈希表键获取了错误的值 import java.util.*; 公共类哈希测试{ //实例变量 字符串名; 智力年龄; int-hashCd; 字符串性别; 公共哈希测试(字符串nm、整数年龄、字符串性别) { 名称=纳米; 这个。年龄=年龄; 性别=性别; } 公共静态void main(字符串[]args){ HashingTest person1=新的HashingTest(“Durvi”,5,“女性”); HashingTest person2=新的HashingTest(“Pillu”,5,“女性”); HashingTest person3=新的HashingTest(“Ninad”,5,“男性”); HashingTest person4=新的HashingTest(“Varun”,5,“男性”); HashingTest person5=新的HashingTest(“Sapna”,5,“女性”); HashingTest person6=新的HashingTest(“Priya”,5,“女性”); //person2和person1现在指的是同一个对象 person2=person1; 布尔真值=person1。等于(person2); println(truth+”:这意味着如果两个对象变量引用同一个对象,equals()方法将返回true); Hashtable hs=新的Hashtable(); hs.put(person1,“Durvi”); hs.put(person2,“Pillu”); hs.put(第三人称“尼纳德”); hs.put(第四人称“瓦伦”); 字符串personVal=(字符串)hs.get(person1); System.out.println(personVal); } }

哈希表键获取了错误的值 import java.util.*; 公共类哈希测试{ //实例变量 字符串名; 智力年龄; int-hashCd; 字符串性别; 公共哈希测试(字符串nm、整数年龄、字符串性别) { 名称=纳米; 这个。年龄=年龄; 性别=性别; } 公共静态void main(字符串[]args){ HashingTest person1=新的HashingTest(“Durvi”,5,“女性”); HashingTest person2=新的HashingTest(“Pillu”,5,“女性”); HashingTest person3=新的HashingTest(“Ninad”,5,“男性”); HashingTest person4=新的HashingTest(“Varun”,5,“男性”); HashingTest person5=新的HashingTest(“Sapna”,5,“女性”); HashingTest person6=新的HashingTest(“Priya”,5,“女性”); //person2和person1现在指的是同一个对象 person2=person1; 布尔真值=person1。等于(person2); println(truth+”:这意味着如果两个对象变量引用同一个对象,equals()方法将返回true); Hashtable hs=新的Hashtable(); hs.put(person1,“Durvi”); hs.put(person2,“Pillu”); hs.put(第三人称“尼纳德”); hs.put(第四人称“瓦伦”); 字符串personVal=(字符串)hs.get(person1); System.out.println(personVal); } },java,Java,输出:: true:这意味着如果两个对象变量引用同一个对象,则equals()方法返回true 皮卢在这种情况下,你的行为是正确的,但是 您不能期望您所做的工作始终正常工作,因为您没有重写 hashcode()和equals() 因为您正在创建一个类,其实例被用作散列容器中的键(如 HashSet、HashMap和HashTable),必须重写hashcode()和equals() 这正按预期工作。您正在做的是: import java.util.*; public class Hashing

输出::

true:这意味着如果两个对象变量引用同一个对象,则equals()方法返回true
皮卢

在这种情况下,你的行为是正确的,但是

您不能期望您所做的工作始终正常工作,因为您没有重写
hashcode()
equals()

因为您正在创建一个类,其实例被用作散列容器中的键(如
HashSet
HashMap
HashTable
),必须重写
hashcode()
equals()


这正按预期工作。您正在做的是:

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);

 }
}
由于
person2==person1
,最后一次调用等于

person2 = person1;

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

String personVal = (String)hs.get(person1);
作为旁注,您需要实现
equals()
hashCode()
对于您的
HashingTest
类,请查看以下内容:


    • 默认情况下,java的equals方法from Object将比较引用。由于您将引用设置为相同,那么是,对“equals”的调用将返回true。如果您希望更改该行为,则需要重写等于以检查字段值(以及您需要的任何其他内容)

      做这件事的时候要小心——我建议你仔细阅读Josh Bloch用有效java所说的话:

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

      鉴于您已将person1和person2变量设置为同一个对象

      Item 8: Obey the general contract when overriding equals
      Item 9: Always override hashCode when you override equals
      
      相当于

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

      请理解,在哈希表中,您使用了对象引用作为键,字符串作为值

      person1和person2都指向主方法中的同一个对象,该对象是由创建的

        hs.put(person1, "Durvi");
        hs.put(person1, "Pillu");
      
      上面的第一个put语句创建一个对象引用为“Durvi”的条目,并为其赋值“Durvi”

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

      因此,当您执行get方法时

      HashingTest("Durvi, 5, Female")
      
      hs.put(person1, "Durvi");
      hs.put(person2, "Pillu");
      
      我希望我已经讲清楚了。。 如果您需要进一步澄清,请返回


      请注意,您使用了对象引用来代替“键”。我希望您没有误会

      直接运行此程序,我想输出结果与预期一致。你的预期产量是多少?谢谢你。。。我刚刚检查了哈希表的大小。。。是3,不是4。现在我知道它被替换了!
      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"