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

查看:130
本文介绍了创建一个hashCode()方法 - 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);
}

说完所有这些,我有几个问题:


  1. 由于我实现了一个新的 equals()方法,我应该实现一个新的<$ c $如何实现一个新的hashCode method()方法?

  2. 有那些变数? (请注意,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 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;
    }
    

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

    That's all!

    解决方案

    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.

    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 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天全站免登陆