Hash Code for a group of three fields

后端 未结 2 1129
别那么骄傲
别那么骄傲 2021-01-28 11:00

I have three fields, namely

  1. Number1
  2. Number2
  3. Time

I am trying to write a function in java that returns a unique hash value (

2条回答
  •  后悔当初
    2021-01-28 11:19

    I take it's a special version of hashCode. Otherwise you would need to overwrite hashCode, don't define a new method. Containers like HashSet don't get your own hash code.

    • So for your specialized version for long, you do not need to use the xor (^) because it's already long. Just use the long value.
    • When using hashCode of String it's not for long, just for int, so it will not "use" all your space. You could duplicate the hashCode of String with longs for your purpose.
    • else looks good.

    (By the way, members should be called with lower letters and Time should be private as well.)

提交回复
热议问题