I understand that to iterate through a Map, I need to use the entrySet() method and then use the Iterator on the resultant Set. I feel the question might be silly, but, how is the Map exactly implemented so that using an Iterator on it directly isn’t possible? As far as I understand, Map isn’t a collection, and Iterator is meant to be used over a collection. But logically, isn’t Map also a collection of key-value pair(though it doesn’t implement the Collection interface)?
I understand that to iterate through a Map , I need to use the
Share
AFAIK, You can’t iterate over a Map directly because there are three types of iteration that a Map supports and there was no clear reason to choose one of the other. Doing the equivalent
entrySet().iterator()may have been reasonable, but it’s not the choice which was made.