HashMap is a Map implementation that uses hashing to store key-value pairs. Internally, it uses an array of buckets and stores entries as linked lists (or trees in case of high collisions in Java 8+). The hashcode of a key determines the bucket index.
Yes, HashMap allows one null key and multiple null values.
The average time complexity of put() and get() operations is O(1). In the worst case (hash collisions), it can degrade to O(log n) (since Java 8 uses tree-based buckets).
- HashMap is not synchronized (faster, not thread-safe).
- Hashtable is synchronized (thread-safe but slower).
- HashMap allows one null key, Hashtable does not.
HashMap resolves collisions using chaining. Initially, collisions are stored as linked lists in a bucket, but from Java 8 onwards, when the list size exceeds a threshold, it is converted into a balanced tree for better performance.
HashMap is a Map implementation that uses hashing to store key-value pairs. Internally, it uses an array of buckets and stores entries as linked lists (or trees in case of high collisions in Java 8+). The hashcode of a key determines the bucket index.
Yes, HashMap allows one null key and multiple null values.
The average time complexity of put() and get() operations is O(1). In the worst case (hash collisions), it can degrade to O(log n) (since Java 8 uses tree-based buckets).
- HashMap is not synchronized (faster, not thread-safe).
- Hashtable is synchronized (thread-safe but slower).
- HashMap allows one null key, Hashtable does not.
HashMap resolves collisions using chaining. Initially, collisions are stored as linked lists in a bucket, but from Java 8 onwards, when the list size exceeds a threshold, it is converted into a balanced tree for better performance.