Java – algorithm for bucket lookup of hash code

See English answers > What hashing function does Java use to implement hashtable class? 5

When the lookup is complete, it goes directly into the bucket and finds (using elements equal to multiple objects in the same bucket)

I've been thinking, how does it go directly into the required bucket? Which algorithm is used for bucket lookup? Does this have any effect on the total lookup time?

Solution

From memory: HashSet is actually supported by HashMap. The basic search process is:

>Get the key > hashcode () > hashcode% number of buckets > go to that bucket and evaluate equals ()

For a set, there are only unique elements I suggest reading the source code of HashSet, which should be able to answer your questions

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/HashMap.java#HashMap.containsKey%28java.lang.Object%29

Please also note that the Java 8 code has been updated. This explanation covers the code base before Java 8 I didn't examine the Java 8 implementation in detail, but found it different

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>