InterviewSolution
| 1. |
Why does not the Map interface extend the Collection Interface or vice-versa? |
|
Answer» If Map extends the Collection Interface, "Key-value pairs" can be the only element type for this type of Collection, although this provides a very limited (and not really useful) Map abstraction. You can't inquire what value a specific key corresponds to, and you can't delete an entry without knowing what value it corresponds to. The three "Collection view PROCEDURES" on MAPS REPRESENT the fact that Maps can be viewed as COLLECTIONS (of keys, values, or pairs) (keySet, entrySet, and values). While it is theoretically feasible to see a List as a Map mapping indices to items, this has the unfortunate side effect of CHANGING the Key associated with every element in the List prior to the deleted member. This is the reason why Collection can not be made to extend the Map Interface either. |
|