Random begining index iterator for HashSet

Posted by funktku on Stack Overflow See other posts from Stack Overflow or by funktku
Published on 2011-01-07T15:35:17Z Indexed on 2011/01/07 15:54 UTC
Read the original article Hit count: 172

Filed under:
|

I use HashSet for add(); remove(); clear(); iterator(); methods. So far everything worked like a charm. However, now I need to fulfill a different requirement.

I'd like to be able to start iterating from a certain index. For example, I'd like the following two programs to have same output.

Program 1

Iterator it=map.iterator();
for(int i=0;i<100;i++)
{
    it.next();
}
while (it.hasNext())
{
  doSomethingWith(it.next());
}

Program 2

Iterator it=map.iterator(100);
while (it.hasNext())
{
  doSomethingWith(it.next());
}

The reason I don't want to use the Program 1 is that it creates un-neccesary overhead. From my researchs, I couldn't not find a practical way of creating an iterator with begining index.

So, my question is, what would be a good way to achieve my goal while minimizing the overheads?

Thank you.

© Stack Overflow or respective owner

Related posts about java

Related posts about data-structures