General answer :
HashMap accept null while Hashtable doesn't,
HashMap is not synchronized,
HashMap is fast and so on along with basics like its stores key and value pairs etc.
Experts answer :
------------ ---- ---------------- ----------------
| |.................| | .................| |....................
------------- --- ---------------- ----------------
:
:
------------ ---- ---------------- ----------------
| |.................| | .................| |........................| Bucket2 | |Node1 | | |
------------- --- ---------------- ----------------
:
:
------------ ---- ---------------- ----------------
| |.................| | .................| |...........................| BucketN | |Node1 | | |
------------- --- ---------------- ----------------
HashMap works on principle of hashing,
we have put(key, value) and get(key) method for storing and retrieving Objects from HashMap.
When we pass Key and Value object to put() method on Java HashMap, HashMap implementation calls hashCode method on Key object and applies returned hashcode into its own hashing function to find a bucket location for storing Entry object, here important point to mention is that HashMap in Java stores both key and value object as Map.Entry in bucket , not only value
What happens when two different objects have same hashcode :
Basically hascode implementation says below rules:
rule 1: If obj1 and obj2 are same it must give same hashcode ,
rule 2: If obj1 and obj2 have same hashcode then they are not need to be equals
Basically Map uses the linkedlist to store Map.Entry object Entry would be having key and value pair
If the different object is also having same hash code then Map will store the Entry object in the same bucket but in the linked list next consequent node
How it retrieves :
When retriving first it gets hash code by calling hashcode() then it identify the target bucket , but when retrieving it calls equals method to get exact object ,if it calls equals method on key object it gives the desired object even if the two different objects having hash code
Which is best practice to implement hashcode and equals ()?
User immutable objects i.e final object with proper equals() and hashcode()
Generally Immutability also allows caching there hashcode of different keys which makes overal process very fast
User wrapper classes like String,Integer...
If used custom objects make it immutable
what happens when resizing HashMap in multithreding scenarios:
Hash map uses the default initial capacity is 16 and load factor is 0.75f
since it is not synchronized it is not suggestable to use in multithreading scenario
if it is really required make it synchronize:
Map map = Collections.synchronizedMap(new HashMap(...))