4

I have two data structures in Java:
One is called DebateAssignment and has 5 DebateTeam objects, each associated with a specific enum that includes

{JUDGE, PROP1, PROP2, OP1, OP2}

In another class I use List<DebateAssignment> and I want to create an iterator that will point to a specific DebateTeam in a specific DebateAssignment and I want it to iterate over all teams over all assignments, going from an assignment to assignment seamlessly.

How would I go about doing that?

3
  • Could you show the code for DebateAssignment ? Feb 19, 2010 at 12:36
  • 4
    I go into brain melt after reading your question.
    – Roman
    Feb 19, 2010 at 12:38
  • I'm guessing that Iterator<DebateAssignment> isn't what you're looking for, you clearly know about it. Can you clarify your question a bit? (If only to refreeze poor Roman's brain?) Feb 19, 2010 at 12:40

4 Answers 4

5

One way, using google-collections / guava:

return Iterables.concat(Iterables.transform(assignments,
    new Function<DebateAssigment, Collection<DebateTeam>>() {
      public Collection<DebateTeam> apply(DebateAssignment assignment) {
        return assignment.getDebateTeams();
      }
    }));

Another way is to store the data as a Multimap<DebateAssignment, DebateTeam>, then simply iterate over either the values() or entries() view. That data structure won't model the JUDGE/PROP1/etc. association, though.

4

Assuming DebateAssignment has something like

public Collection<DebateTeam> getDebateTeams();

You want an Iterator<DebateTeam>?

If so, would you want something like:

public class DebateTeamIterator implements Iterator<DebateTeam> {
    private Iterator<DebateAssignment> iAssignment;
    private Iterator<DebateTeam> iTeam;

    public DebateTeamIterator(Iterator<DebateTeam> iAssignment) {
        this.iAssignment = iAssignment;
        if (iAssignment.hasNext())
            iTeam = iAssignment.next().getDebateTeams().iterator();
        else
            iTeam = new LinkedList<DebateTeam>().iterator();
    }

    public boolean hasNext() {
       return iTeam.hasNext() || iAssignment.hasNext();
    }

    public DebateTeam next() {
        if (!iTeam.hasNext())
            iTeam = iAssignment.next().getDebateTeams().iterator();
        return iTeam.next();
    }

    // ... other methods removed for brevity...
}
2
  • 1
    I can easily be accused of extreme bias, but I don't think it's good advice to suggest pasting this many lines of code, that are this nontrivial and unproven, as opposed to (you guessed it) using a library that already sunk many person-days into exhaustively testing and fixing their implementation. Feb 19, 2010 at 16:59
  • Thanks! Never implemented an iterator before, this answer was really helpful! Feb 19, 2010 at 17:33
2

Probably the easiest approach is:

List<DebateAssignment> list = ...
List<DebateTeam> dtList = new ArrayList<DebateTeam>();
for (DebateAssignment da : list) {
    dtList.addAll(da.getTeams());
}
return dtList.iterator();

Of course you could write a new class that implements Iterator<DebateTeam> that "flattens" nested iterators, but that is somewhat involved, as you will have to explicitly keep track of both iterators ... (see Clinton's answer for details on this).

0

You can extend a list implementation like ArrayList and add a method to return an Iterator implementation which does what you want.

Overriding the iterator() method may not be an option doe to the generic type of the iterator returned by it.

2
  • Actually I didn't understand what he really need but anyway extending collections is a bad practice. You can create a wrapper class around ArrayList and decorate some methods like iterator() (i.e. it's a Decorator pattern).
    – Roman
    Feb 19, 2010 at 12:44
  • If he really wants a first class List I believe it is significantly simpler to go with extension because for composition he'd have to implement all the List methods and delegate them to the actual List. I do agree that the requirement is pretty weird. Feb 19, 2010 at 12:48

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.