Reputation: 3890
what is order of finding correct bucket in java hashmap??
In hashmap first bucket is located using hashcode method and then we iterate over it using equals method, so my question is on first part, what is complexity of finding the bucket in which desired key is present.
Upvotes: 1
Views: 202
Reputation: 86
This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
Upvotes: 0
Reputation: 234867
Looking up the bucket is O(1). Hashmap just computes the hashcode and uses it to index into the bucket slots.
Upvotes: 1