How to use two numbers as a Map key

前端 未结 8 1992
天命终不由人
天命终不由人 2020-12-17 20:32

I have two numbers and I want to use them together as a key in a Map. Currently, I\'m concatenating their string representations. For example, suppose the key n

8条回答
  •  执念已碎
    2020-12-17 20:55

    A practical answer to this questions is:

    hashCode = a + b * 17;
    

    ... where a, b and hashCode are all ints. 17 is just an arbitrary prime number. Your hash will not be unique, but that's OK. That sort of thing is used all over the Java standard library.

提交回复
热议问题