Java - Collections - Interview Questions and Answers on HashMap and HashTable

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.  can we create a null as a key for a map collection ?

Ans. Yes , for HashMap. HashMap implements Map interface. HashMap allows one null key and any number of null values.

Q4.  What is the use of hashcode in Java ?

Ans. Hashcode is used for bucketing in Hash implementations like HashMap, HashTable, HashSet etc. The value received from hashcode() is used as bucket number for storing elements. This bucket number is the address of the element inside the set/map. when you do contains() then it will take the hashcode of the element, then look for the bucket where hashcode points to and if more than 1 element is found in the same bucket (multiple objects can have the same hashcode) then it uses the equals() method to evaluate if object are equal, and then decide if contain() is true or false, or decide if element could be added in the set or not.

Q5.  Difference between Map and HashMap ?

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

Q7.  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.

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?

Ans.AbstractMap

Q17. Which is the Parent Class of PrinterStateReasons class?

Ans.HashMap

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

hMap.put(a,a);
hMap.put(b,b);

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.

Q19.  Difference between Map and HashMap ?

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

Q20.  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.

Q21.  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.


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

Ans. LinkedHashMap

Q23.  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.

Q24.  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.

Q25.  Is this Polymorphism ?

Map<String, List<String>> inventoryManagerCountMap = new HashMap<String, ArrayList<String>>();

Ans. No. This will result in compilation error as Polymorphism cannot be performed on Object types.  

Q26.  Name few Concurrent Collection Classes ?

Ans. ConcurrentHashMap
ConcurrentLinkedDeque
ConcurrentLinkedQueue
ConcurrentMap
ConcurrentNavigableMap
ConcurrentSkipListMap
ConcurrentSkipListSet

Q27.  Name few Collections Map implementations ?

Ans. AbstractMap
ConcurrentHashMap
ConcurrentSkipListMap
EnumMap
HashMap
IdentityHashMap
LinkedHashMap
SystemFlavorMap
TreeMap
WeakHashMap

Q28.  Difference between HashMap and WeakHashMap ?

Ans. WeakHashMap uses weak reference for keys, which means if the key object doesn't have any reference then both key/value mapping will become eligible for garbage collection.

Q29.  Which of the following collection maintain its elements in Natural Sorted order ? 

 a. HashMap
 b. TreeMap
 c. LinkedHashMap
 d. LinkedMap

Ans. TreeMap

Q30.  Which of the following collections stores its elements in insertion Order ?

 a. HashMap
 b. TreeMap
 c. LinkedHashMap
 d. LinkedMap

Ans. LinkedHashMap

Q31.  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.

Q32.  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.

Q33.  Which of the collections allows null as the key ?

 a. HashTable
 b. HashMap
 c. TreeMap
 d. LinkedHashMap

Ans. HashTable