What should I use as a hash code of null?

4.3k Views Asked by At

Assume we have a following trivial class:

public class Foo {

    public Integer bar;

}

And we want to build a "good" hashCode method for it. By "good" I would, for instance mean that there's a small probability of hash code collision in "real-life" cases.

In "real-life" for such a class I would reasonably expect Foos with bar set to null or 0. I'd even argue that these two are probably the most frequent values.

But let's take a look at what Eclipse, for instance, generates:

public class Foo {

    public Integer bar;

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((bar == null) ? 0 : bar.hashCode());
        return result;
    }
}

And it's not just Eclipse, it seems like using 0 as hashCode for null is a normal practice.

But this would produce identical hash codes for null and 0, wouldn't it? An as I'm assuming that null and 0 are probably the most frequent cases - this leads to a higher probabiliy of collission.

So here comes my question. What would be a good hashCode value for null?

3

There are 3 best solutions below

1
On BEST ANSWER

From Joshua Bloch's excellent book Effective Java, 2nd Edition (page 49):

If the value of the field is null, return 0 (or some other constant, but 0 is traditional).

So you can use any constant of your choice, but usually, 0 is used as the hash code of null.

In your case, where 0 appear frequently, it might indeed be better to choose a different constant than 0 (one that doesn't appear as a valid value in your field) to avoid collisions.

2
On

Stick with whatever Eclipse produces, or 0.

If testing or profiling shows that changing the hash code for null would improve performance, go for it. Change it to any arbritrary constant.

0
On

Finding a constant which doesn't appear often might be difficult. You can use a negative constant to avoid conflict with null.