Why can't we use Iterator on a Map(Java)?


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)?


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.


Do you want to iterate the entries (key+value) or only keys or only values? That's why you obtain the specific collection and obtain an iterator.

Since keys/entries are unique (no duplicate elements present), you obtain a set through entrySet() and keySet(). And since values can have duplicates, you obtain the collection through values() and then get an iterator from these.


Collection implements Iterator, and Map though <strong>doesnt fall into</strong> java.util.Collection, its still considered in the Collection framework.

This is what i use to iterate a Map

for (Map.Entry<Integer, Integer> entry : map.entrySet()) { System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); }


  • Ember Handlebars Iterate object and display key inside nested each helper
  • Is boost::filesystem::directory_iterator invalidated by deletion?
  • How to remove the marker in Google map v2?
  • Index of vector iterator
  • java.lang.NullPointerException: No FileItemFactory has been set
  • Play Framework 2.0 correct way to represent a set in a query using Anorm
  • Add filename and length parameter to WCF stream when Transfermode = Stream
  • check if numbers have the same sign
  • Sorting a HashMap, while keeping duplicates
  • Deploying pre-encrypted configuration files to a production environment
  • Each Radiobutton for each form or 1 Form for all radiobuttons?
  • Does SmartGit support git-svn?
  • What does a hyphen at end of a term mean
  • Filter on CALayer except for a shape which is an union of (non necessarily distinct) rectangles
  • Search files(key) in s3 bucket takes longer time
  • Iterate twice through a DataReader
  • Simplify where clause with repeated associated type restrictions
  • custom string delimiters stringtemplate-4
  • Comparing user's facebook/twitter friends to site's users in Python/Django
  • Why can't UI components be accessed from a backgroundworker?
  • SIP API media codecs
  • Use sed with regex and (
  • Where these are stored?
  • abstracting over a collection
  • How can I tell a form not to dispose a particular control when it closes?
  • Cast between interfaces whose interface signatures are same
  • calculate gradient output for Theta update rule
  • Firefox Extension - Monitor refresh and change of tab
  • Insert new calendar with SyncAdapter- Calendar API Android
  • Django simple Captcha “No module named fields” error
  • MongoDb aggregation
  • C++ Partial template specialization - design simplification
  • Update CALayer sublayers immediately
  • Javascript simulate pressing enter in input box
  • C# - Serializing and deserializing static member
  • Bug in WPF DataGrid
  • Resize panoramic image to fixed size
  • Incrementing object id automatically JS constructor (static method and variable)
  • retrieve vertices with no linked edge in arangodb
  • How can I get HTML syntax highlighting in my editor for CakePHP?