13

I often auto-generate an class's hashCode() method using IntelliJ IDEA and typically the method takes the form:

result = 31 * result + ...

My question is what is the purpose of multiplying by 31? I know this is a prime number but why pick 31 specifically? Also, if implementing a hashCode() for a particularly small / large dataset would people approach this problem differently?

1 Answer 1

22

Multiplying by 31 is fast because the JIT can convert it to a shift left by 5 bits and a subtract:

x * 31 == (x << 5) - x

Without any particular extra information, I'd stick to this approach. It's reasonably fast and likely to end up with reasonably well-distributed hash codes, and it's also easy to get right :)

The size of the dataset doesn't really matter, but if you have particular extra information about the values you'll be work with (e.g. "it's always even") then you may be able to design a better hash function. I'd wait until it's an actual problem first though :)

Sign up to request clarification or add additional context in comments.

7 Comments

Then why not 7? That's a shift by 3 and a subtract. And it's a prime
Thanks Jon. If this is the reason it's strange that IDEA doesn't just put (x << 5) - x in the generated code. Is the JIT likely to actually spot this optimisation?
7 allows strings that differ only in two adjacent characters to often end up with the same hash code. In fact pretty much any processor from the last decade or two should be able to manage a multiply by an eight bit number (so long as it is in a register) in a cycle.
Last time I checked 31 was prime, too.
@dma_k: I'm afraid I don't know the details of it... only that it's meant to work well. (I thought Effective Java suggested 31 actually... maybe it's the second edition which does so?)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.