Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provide an iterator over the contents of two lists simultaneously?

Suppose I have this:

public class Unit<MobileSuit, Pilot> {      ...      List<MobileSuit> mobileSuits;     List<Pilot> pilots;      ... } 

And I would like to iterate through the pair of each in the simplest way outside of that class. How should I go about doing that? I thought about doing this:

public class Unit<MobileSuit, Pilot> {      ...     Iterator<MobileSuit> iteratinMechas;     Iterator<Pilot> iteratinPeople;      class IteratorCustom<MobileSuit, Pilot> implements Iterator {          public boolean hasNext() {             return iteratinMechas.hasNext() && iteratinPeople.hasNext();         }          public void remove() {             iteratinMechas.remove();             iteratinPeople.remove();         }          public Object next() {             // /!\         }      }      public Iterator iterator() {         return new IteratorCustom<MobileSuit, Pilot>(mobileSuits, pilots);     } } 

Something along those lines.

Anyway, the problem is that I can't really return just a single object from next(), and I also can't have a Iterator take more than one type. So, any thoughts?

Also, I can't make a new class to combine MobileSuit and Pilot. I need to keep them separate, even though I'm iterating through both at a time. The reason is that there might be Mobile Suits that have no pilots, and I'm not sure how to fix that by keeping them at the same class. This class needs to be processed in other places, so I'd have to unify a interface around that and a lot of other stuff. Basically, assume MobileSuit and Pilot need to be separated.

like image 263
Setsuna F. Seiei Avatar asked Jun 29 '10 04:06

Setsuna F. Seiei


People also ask

Can you iterate through two lists simultaneously Python?

Use the zip() Function to Iterate Over Two Lists in Python Python zip function enables us to iterate over two or more lists by running until the smaller list gets exhausted. The zip function accepts multiple lists, strings, etc., as input.

Can you iterate over lists?

Iterating over a list can also be achieved using a while loop. The block of code inside the loop executes until the condition is true. A loop variable can be used as an index to access each element.

How do you iterate a function over a list?

We can iterate through a list by using the range() function and passing the length of the list. It will return the index from 0 till the end of the list. The output would be the same as above.


1 Answers

Anyway, the problem is that I can't really return just a single object from next(), and I also can't have a Iterator take more than one type. So, any thoughts?

Obviously you are going to need a light-weight "pair" class. This is roughly analogous to the Map.Entry inner class.

Here's a rough cut at a generic solution:

public class ParallelIterator <T1, T2> implements Iterator<Pair<T1, T2>> {      public class Pair<TT1, TT2> {         private final TT1 v1;         private final TT2 v2;         private Pair(TT1 v1, TT2 v2) { this.v1 = v1; this.v2 = v2; }         ...     }      private final Iterator<T1> it1;     private final Iterator<T2> it2;      public ParallelIterator(Iterator<T1> it1, Iterator<T2> it2) {          this.it1 = it1; this.it2 = it2;     }      public boolean hasNext() { return it1.hasNext() && it2.hasNext(); }      public Pair<T1, T2> next() {         return new Pair<T1, T2>(it1.next(), it2.next());     }      ...  } 

Note: this doesn't explicitly deal with cases where the lists have different lengths. What will happen is that extra elements at the end of the longer list will be silently ignored.

like image 81
Stephen C Avatar answered Sep 30 '22 22:09

Stephen C