Java iterator


I am a newbie, I have a question.

I have a map. I have to loop through the map and build the iterator.


public Iterable<Test> getTests(Map<String, Test> testMap,
    Set<String> strings)
    //loop tru the set of strings and build iterator.
    for(final String test1 : strings)

       Test test = testMap.get(test1);

       //build a iterator. not a list.

    return iterator

How can I do this?

Best Solution

First of all, your method is returning an Iterable, not an Iterator. Map, Set, and List all implement Iterable, so it might be easier than you think.

Second, an Iterable is merely a class that has an iterator() method which returns an Iterator.

So I would simply build a List of the results and then return it. If you really want to return an Iterator, I would instead call iterator() on the list and return that.