How to use two numbers as a Map key

前端 未结 8 1974
天命终不由人
天命终不由人 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

    Why should writing all that extra code to make a full blown class that you don't need for anything else be better than using a simple String? Will computing the hash code for instances of that class be much faster than for the String? I don't think so.

    Unless you are running in an extremely limited computing power environment, the overhead of making and hashing Strings should not be noticeably larger than that of instantiating your custom class.

    I guess the fastest way would be to simply pack the ints into a single Long as ZZ Coder suggested, but in any case, I don't expect the speed gains to be substantial.

    0 讨论(0)
  • 2020-12-17 20:58

    You need write the right eqauls and hashcode methods , or produce some bugs.

    0 讨论(0)
提交回复
热议问题