0

I have a list (an ArrayList, infact) of type HColumn<ColName, ColValue>. Now I want to implement an iterator() that iterates over this collection such that on iteration it gives out the corresponding ColValue from each HColumn.

This object HColumn<ColName, ColValue> is defined in an external library used by my java application.

How can I do that, if possible ?

Currently, to create such an iterable, I had been creating a new list altogether containing corresponding ColValues which I guess is not good thing, in terms of performance & efficiency.

0

1 Answer 1

4

As suggested by @jordeu:

public class IteratorColValueDecorator implements Iterator<ColValue> {
      private Iterator<HColumn<ColName, ColValue>> original;
      //constructor taking the original iterator
      public ColValue next() {
           return original.next().getValue();
      }
      //others simply delegating
}

Or, my original suggestion:

public class ColValueIterator implements Iterator<ColValue> {
    private List<HColumn<ColName, ColValue>> backingList;
    //constructor taking List<...>
    int currentIndex = 0;
    public ColValue next() {
        return backingList.get(currentIndex++).getColumn();
    }
    //hasNext() implemented by comparing the currentIndex to backingList.size();
    //remove() may throw UnsupportedOperationException(), 
    //or you can remove the current element
}
Sign up to request clarification or add additional context in comments.

2 Comments

I would simply do an iterator wrapper, so I would get a Iterator<HColumn<ColName, ColValue>> on the constructor.
yes, that's a better option. I'm adding it. 6 hours waiting at the airport have obviously crippled my OOP thinking :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.