Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementation of next() of Iterator over HashMap  RSS feed

 
Mainak Goswami
Ranch Hand
Posts: 52
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to implement a custom iterator using next() over the HashMap. I guess the Iterator provides an abstract view of the collections including the list, array, etc. Basically I am trying to implement the logic inside the next() something like:


Please advise.
Thanks.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can hold an iterator as a data member of your class and call the iterator's next method from your next method.

Needless to say the next method is extremely thread unsafe
 
Mainak Goswami
Ranch Hand
Posts: 52
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jayesh.

To give you little bit more background I am trying to implement iterator design pattern which must consist of ConcreteIterator implementation class. SO I have to provide an implementation of the next(), hasNext() and remove(). I am using a HashMap collection as input parameter.

Once these are done I will call the createIterator from the ConcreteCollection class. Hope I am making some sense.

Please advise if you can suggest a better approach.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should the iterator iterate over? Keys, values?

Like Jayesh said, you can delegate to either keySet().iterator(), values().iterator() or even entries().iterator(). I'm not sure what thread safety has to do with it though. This should be none of your concern.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mainak Goswami wrote:To give you little bit more background I am trying to implement iterator design pattern which must consist of ConcreteIterator implementation class. SO I have to provide an implementation of the next(), hasNext() and remove(). I am using a HashMap collection as input parameter.

Once these are done I will call the createIterator from the ConcreteCollection class. Hope I am making some sense.

Not really. For starters, a HashMap is not a Collection (at least not in the Java sense), it is a Map; and trying to create some sort of catchall "Collection" class or interface that covers both regular Collections and Maps is likely to lead to misery, since they're fundamentally different - unless you're simply viewing your HashMap as a Set of entries.

I guess my first question is: is this HashMap that you're using your own creation, or specifically java.util.HashMap?

Winston
 
Mainak Goswami
Ranch Hand
Posts: 52
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston. Got your point.
My HashMap is java.util.HashMap. Please find below what I was trying to do. I was trying to implement the Iterator design pattern.
a) create the Iterator interface....which in this case will be java.utl.iterator
b) create a child class which implements the Iterator interface. Also this class will take the Collection object as an input and run the implemented operations like hasNext(), next() and remove() method. Need to write custom code which will do the iteration to next elements.
c) create Collection interface with createIterator interface.
d) Create child implementation which will implement the createIterator interface by running over the HashMap. We can view the HashMap as a Set.
The HashMap will contain rows of a Key, value pair.

Hope this explains the requirement in detail. Please do advise of a better alternative.

 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at the Java source code to see how java.util.HashMap's Iterator has been implemented. You may well be able to base your Iterator on that code.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems problematic though. HashMap already has an iterator, and it makes use of private methods and fields to access the data in the map. Your own custom iterator won't be able to do this. There are ways you can solve the problem without this, but frankly they're pretty silly and inefficient. To me, it makes sense to either (a) use HashMap's own Iterator to iterate a java.util.HashMap, or (b) make your own hash map class, and make your own iterator for that class. The second makes sense only if you're doing it for a class or other personal learning experience. So - is this for an assignment of some sort? Why do you want to do this?
 
Mainak Goswami
Ranch Hand
Posts: 52
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That explains all. Thanks Mike and Tony.
Yes, indeed this is just a personal learning and experiment I was trying to implement iterator design pattern using HashMap - possibly an unsuccessful one as I can see. But your explanation was definitely a very good learning.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!