How to write hashCode method for a particular class?

梦想与她 提交于 2019-12-02 22:46:35

To implement hashCode, you override the default implementation from Object:

@Override
public int hashCode()
{
    return row ^ col;
}

This isn't really an ideal hash, since its results are very predictable and it is easy for two different Coord objects to return the same value. A better hash would make use of the built-in Arrays class from java.util (http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html):

@Override
public int hashCode()
{
    return Arrays.hashCode(new Object[]{new Character(row), new Character(col)});
}

You can use this method to generate a pretty good hash with any number of fields.

To implement compareTo, you'll want your class to implement Comparable:

public class Coord implements Comparable<Coord>

Once you've done this, you can make compareTo take an argument of type Coord rather than type Object, which will save you the trouble of checking its type.

Hashcode is an int (32 bits), your data is char (16 bits), so I would probably just do:

@Override
public int hashCode() {
    return (row << 16) + col;
}

This puts the bits from row in the first 16 bits and the bits from col in the last 16 bits, so this is a perfect hash function for this class.

If you refactor your class to be more complicated, I recommend using nullptr's answer.


To use Comparable, do:

public class Coord implements Comparable<Coord>

I found very valuable information concerning this topic and many other topics in the Effective Java book, written by Joshua Bloch. Look at page 45 for further information about hashCode() and equals().

If you use an IDE like Eclipse you can let it generate the hashCode() and equals() methods. For your class the result would be:

class Coord implements Comparable<Coord> {

    private char row;
    private char col;

    public Coord(char x, char y) {
        row = x;
        col = y;
    }

    public Coord() {
    };

    public char getX() {
        return row;
    }

    public char getY() {
        return col;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + col;
        result = prime * result + row;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Coord other = (Coord) obj;
        if (col != other.col)
            return false;
        if (row != other.row)
            return false;
        return true;
    }

    public int compareTo(Coord param) {
        // Implementation according to http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
        return 0;
    }

}

similar to durron597's answer, you can try this if your input is bounded by char (between 0 and 65535 )

public int hashCode(){
   return row * 100000 + col;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!