Why does hashcode() returns an integer and not long? [duplicate]

自作多情 提交于 2019-12-01 23:04:04

问题


In java, hashcode() method, returns integer instead of long. Is there any specific reason?


回答1:


Well, one good reason is that the hashCode based data structures (HashSet, HashMap) use an array to store the bins, and arrays are limited to int indices. You will gain nothing by a long hashCode() if you must map it to an int array index.




回答2:


Using the hashCode is to have N buckets where the hashCode % N determines the bucket of elements, hopefully being 1 (no conflicting hashCodes).

For N, for the hash code, an int is entirely sufficient; indeed one needs to have most variety in the lower bits; having a long without using the higher bits (when N would be a power of 2), would be counter productive.

Speed of course is a requirement too: int being slightly better on the final CPU.



来源:https://stackoverflow.com/questions/49489116/why-does-hashcode-returns-an-integer-and-not-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!