创建一个hashCode()方法 - Java [英] Creating a hashCode() Method - Java
问题描述
我为创建的类编写 hashCode()
方法遇到了一些麻烦。这个类意味着在TreeSet中使用,因此它实现了Comparable。该类有以下变量:
public class Node实现了Comparable< Node> {
矩阵矩阵;
int [] coordinates = new int [2];
节点父;
int深度;
诠释成本;
以下是 compareTo()
方法。我希望 TreeSet
按它们的代价组织这些节点结构,因此, compareTo()
返回一个简单的结果减法。
public int compareTo(Node nodeToCompare){
return this.cost - nodeToCompare.cost;
}
我还实现了一个 equals()
方法。
public boolean equals(Object objectToCompare){
if(objectToCompare == this){返回true;}
if(objectToCompare == null || objectToCompare.getClass()!= this.getClass()){return false;}
节点objectNode =(Node)objectToCompare;
返回this.father.equals(objectNode.father)&&
this.depth == objectNode.depth&&
this.cost == objectNode.cost&&
this.matrix.equals(objectNode.matrix)&&
Arrays.equals(this.coordinates,objectNode.coordinates);
}
说完所有这些,我有几个问题:
- 由于我实现了一个新的
equals()
方法,我应该实现一个新的<$ c $如何实现一个新的hashCodemethod()
方法? - 有那些变数? (请注意,Matrix类型的变量矩阵实现了
hashCode()
方法)
这就是全部!
解决方案您的
compareTo
方法不是与您的等于
方法一致:您的compareTo
方法表示如果两个实例具有相同的费用
—这样一个TreeSet
最多只能包含一个给定成本
的实例—但是等于
方法表示它们只有在它们具有相同的成本
和
所以,假设你的
>等于
方法是正确的:
- 您需要修正您的
compareTo
方法与其保持一致。 - 你需要创建一个与它一致的
hashCode
方法。我建议使用与java.util.List.hashCode()
,这是一种直接和有效的方法来组装对象的散列码特定的顺序;基本上你会写如下: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);
返回hashCode;
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;
Here's the implementation of the
compareTo()
method. I want theTreeSet
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; }
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:
- Since I implemented a new
equals()
method, should I implement a newhashCode()
method? - How can I go about implementing a new hashCode
method()
with those variables? (Note that the variable matrix of the type Matrix has ahashCode()
method implemented)
That's all!
解决方案Your
compareTo
method is not consistent with yourequals
method: yourcompareTo
method says that two instances are equivalent if they have the samecost
— such that aTreeSet
can only ever contain at most one instance with a givencost
— but yourequals
method says that they're only equivalent if they have the samecost
and are the same in various other ways.So, assuming that your
equals
method is correct:- 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 byjava.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屋!
查看全文 - 您需要修正您的