获取类似内容但不参考的 Hashtable Key

Get Hashtable Key of similar content but not reference

我有一些 java 代码如下所示:它使用 Hashtable 来存储对应于三维点的数据。

Hashtable<ThreeDimensionalPoint,data> table = new Hashtable<ThreeDimensionalPoint,data>();
table.put(new ThreeDimensionlPoint(1,1,1),new data());
table.get(new ThreeDimensionalPoint(1,1,1);

我想做的是把data从哈希表中取出来,只知道ThreeDimensionalPoint对象的坐标。当然,第三行将不起作用,因为 .get 通过引用匹配键,而不是对象的内容。

有人有解决方案吗?ThreeDimensionalPointclass 有三个指定坐标的整数。

您需要在您的 class ThreeDimensionalPoint 中重写 equals 和 hashcode 方法,就像这个例子(随意将其调整为您的确切代码):

class ThreeDimensionalPoint 
{

    public int cord1;
    public int cord2;
    public int cord3;

    ...

    @Override
    public boolean equals(Object obj)
    {
        // checking if both the object references are 
        // referring to the same object.
        if(this == obj)
            return true;

        // it checks if the argument is of the 
        // type ThreeDimensionalPoint by comparing the classes 
        // of the passed argument and this object.
        if(obj == null || obj.getClass()!= this.getClass())
            return false;

        // type casting of the argument. 
        ThreeDimensionalPoint threeDPoint = (ThreeDimensionalPoint) obj;

        // comparing the state of argument with 
        // the state of 'this' Object.
        return (threeDPoint.cord1 == this.cord1 && threeDPoint.cord2 == this.cord2 && threeDPoint.cord3 == this.cord3);
    }

    @Override
    public int hashCode()
    {
        int hash = 7;
        hash = 31 * hash + cord1;
        hash = 31 * hash + cord2;
        hash = 31 * hash + cord3;

        return hash;
    }

}

您可以在此 link 找到有关此示例的更多信息: https://www.geeksforgeeks.org/equals-hashcode-methods-java/

您需要覆盖 ThreeDimensionalPoint class 的 equalshashCode 方法。

假设你的ThreeDimensionalPoint的属性是维度(x,y,z),下面是IntelliJ Idea生成的例子

 @Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Coordinate that = (Coordinate) o;

    if (x != that.x) return false;
    if (y != that.y) return false;
    return z == that.z;

}

@Override
public int hashCode() {
    int result = x;
    result = 31 * result + y;
    result = 31 * result + z;
    return result;
}

When you are overriding equals and hashCode, you should keep in mind that,

(1) If your equals method returns that two objects are the same then your hashCode must return the same code for both of them. Otherwise, you will see unpredictable behavior in your Hashtable.

(2) Although not required, if your equals method returns that two objects are unequal then,hashCode should try to generate two different value for these two objects. This helps to achieve better performance.

不确定我是否正确理解你的问题,但不是使用对象作为键,因为你只跟踪 3 个坐标,为什么不使用字符串作为键?

而不是

Hashtable<ThreeDimensionalPoint,data> table = new Hashtable<ThreeDimensionalPoint,data>();
table.put(new ThreeDimensionlPoint(1,1,1),new data());
table.get(new ThreeDimensionalPoint(1,1,1));

为什么不

String tag = 1 + "-" + 1 + "-" + 1; // making it "1-1-1"
Hashtable<String,data> table = new Hashtable<String,data>();
table.put(tag,new data());
table.get("1-1-1");