I read in a book where it was mentioned that when we put elements in HashMap, internally it is stored in bucket. My question is
-
Does hashmap store key-value pair altogether in the form of linked list? or does it store in linked list only when there is a collision?
-
How does it retrieve the object when 2 different objects are stored in the same bucket?
Thanks!
Lots of details at http://en.wikipedia.org/wiki/Hash_table
See also Internal implementation of java.util.HashMap and HashSet
And of course you can use the source, Luke.
Updated: to specifically answer your Q, it stores an Entry, which has a reference to the next item in the bucket (if any). If there is only one item in the bucket then the reference will be null: