Java - Collections - Interview Questions and Answers on Maps

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.  What is the difference between List, Set and Map ?

Ans. List - Members are stored in sequence in memory and can be accessed through index. 
Set - There is no relevance of sequence and index. Sets doesn't contain duplicates whereas multiset can have duplicates. Map - Contains Key , Value pairs.

Q3.  Which interface does java.util.Hashtable implement?

Ans. Java.util.Map

Q4.  Which interface provides the capability to store objects using a key-value pair?

Ans. java.util.map

Q5.  Difference between HashMap and Hashtable?

Ans. Hashtable is synchronized whereas HashMap is not.

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

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

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

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

Q8.  Difference between Map and HashMap ?

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

Q9.  What is a Property class ?

Ans. The properties class is a subclass of Hashtable that can be read from or written to a stream.

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

Q11.  How can we reverse the order in the TreeMap ?

Ans. Using Collections.reverseOrder()

Map<Float,Integer> tree = new TreeMap<Float,Integer>(Collections.reverseOrder());

Q12.  TreeMap orders the elements on which field ?

Ans. Keys

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

Q14.  Name few Collections Map implementations ?

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

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

Ans. TreeMap

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

Ans. LinkedHashMap

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

Ans. The new element will replace the existing element.

Q18.  Which of the following is false ?

Ans. HashMap came before HashTable.

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

Ans. HashTable

Q20.  Effective Java 6 , TreeMap implements ...

Ans. SortedMap Interface

Q21. Which interfaces are implemented by  RenderingHints?

Ans.[Map]

Q22. Which is the Parent Class of PrinterStateReasons class?

Ans.HashMap

Q23. Which is the Parent Class of HashMap class?

Ans.AbstractMap

Q24. Name few classes that implement Map interface?

Ans.[ LinkedHashMap,  TabularDataSupport,  Attributes,  PrinterStateReasons,  SimpleBindings,  IdentityHashMap,  Properties,  WeakHashMap,  TreeMap,  ConcurrentSkipListMap,  AuthProvider, AbstractMap,  ConcurrentHashMap,  Hashtable,  EnumMap,  RenderingHints,  UIDefaults,  Provider,  HashMap]

Q25. Which interfaces are implemented by PrinterStateReasons?

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

Q26. Which interfaces are implemented by HashMap?

Ans.[Serializable,  Map,  Cloneable]