Building a custom iterator.
Posted
by
Isai
on Stack Overflow
See other posts from Stack Overflow
or by Isai
Published on 2011-03-06T22:48:49Z
Indexed on
2011/03/07
0:10 UTC
Read the original article
Hit count: 152
I am making this class which is a custom Map based off a hash map. I have an add method where if you add an object the object will be the key, and its value will be 1 if the object is not currently in the list. However if you add object that is currently in the list its value will be bumped up by 1. So if I added 10 strings which were all the same, the key would be that string and the value will be 10. I understand in practice when I iterate through the map, there is actually only one object to iterate, however, I am trying to create a inner class that will define an iterator that will iterate the same object however many times its value is. I can do this by simply using for loops to construct an appropriate ArrayList and just create an iterator for that, but that is too inefficient. Is there an easy or more efficient way of doing this?
© Stack Overflow or respective owner