创建 hashCode() 方法 - Java [英] Creating a hashCode() Method - Java

查看:16
本文介绍了创建 hashCode() 方法 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为我创建的类编写 hashCode() 方法时遇到了一些问题.此类旨在在 TreeSet 内使用,因此,它实现了 Comparable.该类具有以下变量:

I'm having some trouble writing a hashCode() method for a class I created. This class is meant to be used inside a TreeSet, and as such, it implements Comparable. The class has the following variables:

public class Node implements Comparable<Node> {
   Matrix matrix;
   int[] coordinates= new int[2];
   Node father;
   int depth;
   int cost;

这是 compareTo() 方法的实现.我希望 TreeSet 按成本组织这些 Node 结构,因此,compareTo() 返回简单减法的结果.

Here's the implementation of the compareTo() method. I want the TreeSet to organize these Node structures by their cost, therefore, compareTo() returns the result of a simple subtraction.

public int compareTo(Node nodeToCompare) {
    return this.cost - nodeToCompare.cost;
}

我还实现了一个 equals() 方法.

I also implemented an equals() method.

public boolean equals(Object objectToCompare) {
    if(objectToCompare== this) {return true;}
    if(objectToCompare== null || objectToCompare.getClass()!= this.getClass()) {return false;}

    Node objectNode= (Node) objectToCompare;
    return this.father.equals(objectNode.father) &&
            this.depth== objectNode.depth &&
            this.cost== objectNode.cost &&
            this.matrix.equals(objectNode.matrix) &&
            Arrays.equals(this.coordinates, objectNode.coordinates);
}

说了这么多,我还有几个问题:

Having said all of that, I have a few questions:

  1. 既然我实现了一个新的 equals() 方法,我应该实现一个新的 hashCode() 方法吗?
  2. 如何使用这些变量实现新的 hashCode method()?(注意Matrix类型的变量矩阵实现了hashCode()方法)
  1. Since I implemented a new equals() method, should I implement a new hashCode() method?
  2. How can I go about implementing a new hashCode method() with those variables? (Note that the variable matrix of the type Matrix has a hashCode() method implemented)

仅此而已!

推荐答案

您的 compareTo 方法与您的 equals 方法不一致:您的 compareTo 方法表示如果两个实例具有相同的 cost —这样一个 TreeSet 最多只能包含一个具有给定 cost 的实例 —但是您的 equals 方法表示,只有当它们具有相同的 cost 在其他各种方面都相同时,它们才是等价的.

Your compareTo method is not consistent with your equals method: your compareTo method says that two instances are equivalent if they have the same cost — such that a TreeSet can only ever contain at most one instance with a given cost — but your equals method says that they're only equivalent if they have the same cost and are the same in various other ways.

所以,假设您的 equals 方法是正确的:

So, assuming that your equals method is correct:

  • 您需要修正您的 compareTo 方法以与它保持一致.
  • 你需要创建一个与其一致的hashCode方法.我建议使用与 java.util.List.hashCode(),这是一种以特定顺序组装组件对象的哈希码的直接而有效的方法;基本上你会写一些类似的东西:
  • you need to fix your compareTo method to be consistent with it.
  • you need to create a hashCode method that is consistent with it. I recommend using the same sort of logic as is used by java.util.List.hashCode(), which is a straightforward and effective way to assemble the hash-codes of component objects in a specific order; basically you would write something like:
int hashCode = 1;
hashCode = 31 * hashCode + (father == null ? 0 : father.hashCode());
hashCode = 31 * hashCode + depth;
hashCode = 31 * hashCode + cost;
hashCode = 31 * hashCode + matrix.hashCode();
hashCode = 31 * hashCode + java.util.Arrays.hashCode(coordinates);
return hashCode;

这篇关于创建 hashCode() 方法 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆