Q1 How HashMap internally works in java 8? (solution)
This question is the most popular interview question for java developers. The get() method of HashMap works on the principle of Hashing. TreeNode concept is introduced in java 8 to store key-value pairs. You can find the detailed explanation here.
Q2 What is the difference between HashMap and Hashtable in java ? (solution)
1. The main difference between Hashtable and HashMap is that HashMap can contain one null key and any number of null values but Hashtable does not allow null values.
2. Hashtable is synchronized while HashMap is not synchronized.
3. HashMap is faster as compared to Hashtable because HashMap is not synchronized.
Q3 What is the difference between HashMap and ConcurrentHashMap in java ? (solution)
1. ConcurrentHashMap is thread-safe that is at a time only one thread can access the code whereas HashMap is not thread-safe.
2. ConcurrentHashMap does not allow keys to contain null values whereas HashMap can contain one null key.
Q4 What is the threshold value after which bucket converted from linked list to Tree?
If for a given bucket, if there are more than 8 Nodes then the linked list is converted into a Red Black tree. This is represented in the HashMap class code as follows :
static final int TREEIFY_THRESHOLD = 8;
Below image displays both tree (at bucket 0), and linked lists (at bucket 1, 2, 3) . Bucket 0 is a Tree because it contains at least 8 nodes (containing Node A, Node B, Node C, Node D, Node E, Node F , Node K , Node Q, Node O, Node M, Node P) Q5 What is the threshold value after which Tree is converted back to linked list?
If for a given bucket, if there are less than 6 Nodes then the Tree is converted back to the linked list. This is represented in the HashMap class code as follows :
static final int UNTREEIFY_THRESHOLD = 6;
Q6 What is the time complexity of basic operations get() and put() in HashMap class?
According to Java docs,
The HashMap provides the constant time performance O(1) for the basic operations get and put , assuming that the hash function disperses the elements properly among the buckets.
Q7 What will happen if you try to store duplicate key in the HashMap?
If you try to store a key which is already present in the HashMap then it will override the old value with the new value. It will not throw error or exception. The size of the HashMap does not change. This is one of the reason that when you call keySet() method on HashMap to get all keys it will return Set instead of Collection because Set does not allow duplicates.
Q8 What will happen if you store duplicate value in the HashMap?
Yes, HashMap can store duplicate values in java. This is the reason when you call values() method of HashMap to get all values it will return a Collection not a Set. It also does not return List because HashMap does not maintain order guarantee for key and value.
Q9 In HashMap, what is the requirement for an object to be used as key?
Any class(for e.g String) can serve as a key if and only if it overrides the hashCode() and equals() method. hashCode() method is used when you insert a key into the HashMap while equals() method is used to retrieve the value from HashMap.
Q10 What will happen if you try to store null key in HashMap?
Only one null key is allowed in HashMap. If key of the HashMap is null then it will always be present in index 0. NullPointerException is thrown if you try to call hashCode() method on null key.
As a result, when a get() method is called then value of the first index is returned.
Q11 What is the difference between Collections.synchroizedHashMap(HashMap) and HashMap?
The main difference between Collections.synchronizedHashMap(HashMap) and HashMap is that HashMap is non-synchronized while Collections.synchronizedHashMap(HashMap) is a wrapped instance of HashMap which has all put and get methods synchronized.
Q12 What are the different ways to traverse HashMap in java?
There are various ways by which we can iterate HashMap in java :
1. Using keySet() and iterator()
2. Using entrySet() and enhanced for loop
3. Using entrySet() and iterator()
4. Using keySet() and get() method
Q13 What is the difference between capacity and size of HashMap in java?
The capacity indicates the number of entries object a HashMap can store whereas size() denotes how many key-value pairs is currently present in the HashMap.
Q14 What is the difference between HashMap and ArrayList ? (solution)
1. HashMap implements Map interface while ArrayList class implements List interface.
2. HashMap does not provide ordering guarantee where as ArrayList maintains the order of the object in which they are inserted.
3. ArrayList allows duplicate objects whereas HashMap does not allow duplicate keys.
Q15 How will you measure the performance of HashMap?
According to Java docs,
An instance of HashMap has two parameters that affects its performance
1. load factor
2. capacity
load factor : The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.
capacity : The capacity is the number of buckets in the hash table( HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.), and the initial capacity is simply the capacity at the time the hash table is created.
Q16 What is the difference between HashMap and TreeMap in java ? (solution)
1. HashMap does not maintain any ordering guarantee. TreeMap elements are sorted according to the natural ordering of the elements.
2. HashMap implementation internally uses hashing. TreeMap internally uses Red-black tree implementation.
3. HashMap can contain any number of null values and one null key. TreeMap can not contain null keys but may contain null values.
Q17 What is the difference between clear() and remove() method of HashMap?
clear() method removes all the entries object from the HashMap and returns void.
remove() method removes the mapping for the key passed as a parameter.
Q18 How HashMap remove() method internally works in java ? (solution)
That's all for this post HashMap interview questions and answers . If you liked this article then please share it with your friends. If you have any questions or doubts then feel free to mention in the comments.