Java - Interview Questions and Answers on HashMap

Q1.  Difference between TreeMap and HashMap ?

Ans. They are different the way they are stored in memory. TreeMap stores the Keys in order whereas HashMap stores the key value pairs randomly. 

Q2.  Difference between HashMap and Hashtable?

Ans. Hashtable is synchronized whereas HashMap is not.

HashMap allows null values whereas Hashtable doesn’t allow null values.

Q3.  There are two objects a and b with same hashcode. I am inserting these two objects inside a hashmap.


where a.hashCode()==b.hashCode()

Now tell me how many objects will be there inside the hashmap?

Ans. There can be two different elements with the same hashcode. When two elements have the same hashcode then Java uses the equals to further differentation. So there can be one or two objects depending on the content of the objects.

Q4.  can we create a null as a key for a map collection ?

Ans. Yes , for Hashtable. Hashtable implements Map interface.

Q5.  Difference between Map and HashMap ?

Ans. Map is an interface where HashMap is the concrete class.

Q6.  Why String is popular HashMap key in Java?

Ans. Since String is immutable, its hashcode is cached at the time of creation and it doesn’t need to be calculated again. This makes it a great candidate for key in a Map and it’s processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys.

Q7.  Can you provide some implementation of a Dictionary having large number of words ? 

Ans. Simplest implementation we can have is a List wherein we can place ordered words and hence can perform Binary Search.

Other implementation with better search performance is to use HashMap with key as first character of the word and value as a LinkedList.

Further level up, we can have linked Hashmaps like ,

hashmap {
a ( key ) -> hashmap (key-aa , value (hashmap(key-aaa,value)
b ( key ) -> hashmap (key-ba , value (hashmap(key-baa,value)
z( key ) -> hashmap (key-za , value (hashmap(key-zaa,value)

upto n levels ( where n is the average size of the word in dictionary.

Q8.  Which Java collection class can be used to maintain the entries in the order in which they were last accessed?

Ans. LinkedHashMap

Q9.  Can we add duplicate keys in a HashMap ? What will happen if we attempt to add duplicate values ?

Ans. No, We cannot have duplicate keys in HashMap. If we attempt to do so , the previous value for the key is overwritten.

Q10.  What is a ConcurrentHashMap ?

Ans. ConcurrentHashMap is a hashMap that allows concurrent modifications from multiple threads as there can be multiple locks on the same hashmap.

Q11.  How is HashSet maintained in memory by Java ?

Ans. HashSet is maintained as HashMap by Java with values of the HashSet as Keys of the HashMap and value of the HashMap as the constant PRESENT.

Q12.  If we try to add duplicate key to the HashMap, What will happen ?

 a. It will throw an exception.
 b. It won't add the new Element without any exception.
 c. The new element will replace the existing element.
 d. Compiler will identify the problem and will throw an error.

Ans. The new element will replace the existing element.

Q13.  Which of the following is false ?

 a. HashMap came before HashTable.
 b. HashMap allows null values whereas Hashtable doesn’t allow null values.
 c. HashTable and HashMap allow Key-Value pairs.
 d. Hashtable is synchronized whereas HashMap is not.

Ans. HashMap came before HashTable.

Q14. Which interfaces are implemented by HashMap?

Ans.[Serializable,  Map,  Cloneable]

Q15. Which interfaces are implemented by PrinterStateReasons?

Ans.[ PrintServiceAttribute, Serializable, Severity>,  Attribute,  Cloneable,  Map<PrinterStateReason]

Q16. Which is the Parent Class of HashMap class?


Q17. Which is the Parent Class of PrinterStateReasons class?


Q18. What is the package name for HashMap class?

Q19. Which are the subclasses for HashMap?

Ans.[PrinterStateReasons, LinkedHashMap]