【问题标题】:My hashcode will not work - is it something wrong with my equals()?;我的哈希码不起作用 - 我的 equals() 有问题吗?
【发布时间】:2015-05-29 10:48:58
【问题描述】:

每当我尝试运行这个程序时,我都会得到以下输出:

6150

5000

1015612567

列表中的人:

{A 人=100,B 人=50}

下一步:

人物:人物 A 哈希码:507806258 值:100

下一步:

人物:人物 B 哈希码:507806309 值:50

String name;
int day;

static Map<String, Integer> people = new HashMap<>();

public PeopleHash(String name, int day) {
    this.name = name;
    this.day = day;

}

public String getName() {
    return name;
}

public int getDay() {
    return day;
}

public boolean equals(PeopleHash other) {
    if (other instanceof PeopleHash) {
        PeopleHash d = (PeopleHash)other;
        return name == d.name && day == d.day;
    } else {
        return false;
    }
}

@Override
public int hashCode() {
    return 50 * day;
}

public PeopleHash peopleEntry(){
    String namn = "A";
    int schiffre = 123;
    return null;
}


public static void main(String[] args) {


    PeopleHash p1 = new PeopleHash("Isolde", 123);
    PeopleHash p2 = new PeopleHash("Jean", 100);

    people.put("Person A", 100);
    people.put("Person B", 50);



    System.out.println(p1.hashCode());
    System.out.println(p2.hashCode());



    System.out.println("People in list: \n" + people);

    for (Entry<String, Integer> entry : people.entrySet()) {
        String a = entry.getKey();
        int b = entry.getValue();

        System.out.println("Next:");
        System.out.println("Person: " + a + " Hashcode: " + entry.hashCode() + " Value: " + b);

    }

}

}

我做错了什么?

【问题讨论】:

  • 哦,对不起。我没有得到输出“1015612567”。
  • 什么都没有。有什么问题?

标签: java dictionary hashmap hashcode


【解决方案1】:

使用Object.equals(Object) 方法比较对象。您已经定义了一个带有签名 PeopleHash.equals(PeopleHash) 的新方法。

将方法签名改为

@Override
public boolean equals(Object other) {

始终建议添加@Override 注解,因为如果您定义的方法没有覆盖另一个方法,编译器会警告您。

【讨论】:

    【解决方案2】:

    也许equals方法肯定有问题。试试这个 -

    public boolean equals(Object other) {
    
                if (other instanceof PeopleHash) {
               PeopleHash d = (PeopleHash)other;
               return name.equals(d.name) && day == d.day;
      }
       return false;
    }
    

    【讨论】:

    • 我还没有测试你所有的代码,所以如果你仍然遇到问题,请提出问题,我会调查。
    【解决方案3】:

    哈希/等于本身是可以的。问题在于使用(或缺乏)。

    “Person:”输出显示 Map.Entry 对象 (entry.hashCode()) 的哈希码,与 p1/p2 无关。地图中的条目由 String Keys 添加,并且 p1/p2 对象被简单地丢弃。

    Map 可能应该被声明为Map&lt;HashPerson, Integer&gt; 以便使用 HashPerson 的 equals/hashCode。个人对象应直接用作键:

    people.add(p1, 1234);
    

    那么循环可以这样写:

    for (Entry<HashPerson, Integer> entry : people.entrySet()) {
       HashPerson person = entry.getKey();
       Integer value = entry.getValue();
    
       System.out.println(
         String.format("Person %s HashCode: %d Value: %d",
           person.getName(), person.hashCode(), value));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 1970-01-01
      相关资源
      最近更新 更多