Java iterator

I'm a newbie, I have a question.

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

Example:

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?

+1


a source to share


6 answers


First of all, your method returns Iterable

, not Iterator

. Map

, Set

And List

all implement Iterable

, so it can be easier than you think.

Second, Iterable

it's just a class that has a method iterator()

that returns Iterator

.



So, I would just plot the List

results and return it. If you really want to return Iterator

, I would instead type iterator()

in the list and return it.

+6


a source


Overcoming the Iterator / Iterable confusion (and Iterable is basically an Iterator factory ... so you have to write an Iterator anyway), I think you mean something like this:

Iterator<Test> getTests(final Map<String,Test> testMap, final Set<String> strings) {
    return new Iterator<Test>() {
      private final Iterator<String> keyIter = strings.iterator();
      private String lastKey;
      public boolean hasNext() { return keyIter.hasNext(); }
      public Test next() { lastKey = keyIter.next(); return testMap.get(lastKey); }
      public void remove() { testMap.remove(lastKey); }
    };
  }

      

And if you want to return an Iterable, it just has to be a factory for those:



Iterable<Test> getTests(final Map<String,Test> testMap, final Set<String> strings) {
    return new Iterable<Test>() {
        public Iterator<Test> iterator() {
            return new Iterator<Test>() {
                private final Iterator<String> keyIter = strings.iterator();
                private String lastKey;
                public boolean hasNext() { return keyIter.hasNext(); }
                public Test next() { lastKey = keyIter.next(); return testMap.get(lastKey); }
                public void remove() { testMap.remove(lastKey); }
              };
        }
    };
}

      

For extra credit, you can parameterize this method and have a general way to repeat the selection from the card:

Map<String, Action> map;
Set<String> keys;
for (Action x : filterMap(map, keys)) {
}

      

+3


a source


Since Collection

extends Iterable

, the correct solution would be to add tests to some kind of collection, for example ArrayList

.

Collection<Test> tests = new ArrayList<Test>();
for (String key : strings) {
  Test t = testMap.get(key);
  if (t != null)
    tests.add(t);
}
return tests;

      

+2


a source


If you want to really return an Iterable the simplest way is

public Iterable<Test> getTests(Map<String, Test> testMap, Set<String> strings)
{
   testMap = new HashMap<String, Test>(testMap);
   testMap.keySet().retainAll(strings);
   return testMap.values();
}

      

If you want to return an iterator, replace the last line with

return testMap.values().iterator();

      

If you don't care about the map changes you made, skip the initial tempo map structure.

EDIT: Removed using a clone on a map interface that doesn't have it.

+1


a source


Like a blow in the dark, you can search Map.entrySet.iterator()

. This allows you to iterate over the map entries.

Note. I know this is not the answer to the OP's question, but I think it answers what they are actually looking for. If this assumption turns out to be wrong, I will delete this answer again to save people from confusion.

+1


a source


Iterable is different from Iterator . Iterable is something you can iterate over, like List. You use an Iterator for this. Your questions are unclear what you want to return from getTests ().

Your best bet is to create an Iterable (like List or Vector) and return it or return it as an Iterator.

0


a source







All Articles