简体   繁体   中英

Get Hashtable Key of similar content but not reference

I have some java code that looks like this: it uses a Hashtable to store data that corresponds to a three-dimensional point.

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

What I want to do is to get data back out of the hashtable, only knowing the coordinates of the ThreeDimensionalPoint object. Of course, the third line will not work, since .get matches keys by reference, not content of the object.

Does anybody have a solution?the ThreeDimensionalPoint class has three integers that specify the coordinates.

You need to override equals and hashcode methods in your class ThreeDimensionalPoint, like this example (feel free to adjust it to your exact code):

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;
    }

}

You can find more information of this example at this link: https://www.geeksforgeeks.org/equals-hashcode-methods-java/

You need to override the equals and hashCode methods of your ThreeDimensionalPoint class.

Assuming that the properties of your ThreeDimensionalPoint are the dimensions (x, y, z), the following is an example generated from 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.

Not sure if I understand your problem correctly, but instead of using the object as a key, since you are only tracing 3 coordinates, why not make a string as key?

Instead of

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

Why not

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM