Java - Interview Questions and Answers on Hash Collections

Q1.  Difference between HashMap and Hashtable?

Ans. Hashtable is synchronized whereas HashMap is not.

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

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

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

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

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

Ans. LinkedHashMap

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

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

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

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

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

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

Q12. Which is the Parent Class of  HashMap class?

Ans.AbstractMap

Q13. What is the package name for  HashMap class?

Ans.java.util

Q14. Which are the subclasses for HashMap?

Ans.[PrinterStateReasons, LinkedHashMap]