Results 1 to 5 of 5

Thread: Multi-iterators

  1. #1
    philwei is offline Member
    Join Date
    Oct 2011
    Location
    Calgary, Canada
    Posts
    4
    Rep Power
    0

    Post Multi-iterators

    Hi Everyone, this is my first post so hoping someone can help with.

    What I wanted to do is to create an Iterator implementation that has the ability to loop through 2 collections. It means that any object that contains two collections can call "iterator()" to return an iterator that loops through the 1st collection first and then the 2nd one.

    If i call the "hasNext" method of this iterator, it should return true as long as the end of the 2nd collection is not reached.

    If I call "next()" method, the next element should be returned as if the two collections are merged as one.

    The generic types of both collections must be the same.

    The collections do not have to be the same type of collections.

    This is what I did:


    import java.util.*;

    public class DoubleIterator<E> implements Iterator<E>{

    /**
    * The iterator of the first collection.
    */
    private Iterator iteratorA;

    /**
    * The iterator of the 2nd collection.
    */
    private Iterator iteratorB;

    private int current = 0;

    public DoubleIterator(Collection<E> col1, Collection<E> col2){
    iteratorA = col1.iterator();
    iteratorB = col2.iterator();
    }

    public boolean hasNext(){
    if( iteratorA.hasNext() || iteratorB.hasNext() )
    return true;
    else
    return false;
    }

    public E next(){

    if( iteratorA.hasNext() ){
    current = 0;
    return (E)iteratorA.next();
    }else{
    current = 1;
    return (E)iteratorB.next();
    }

    }

    public void remove(){
    if( current == 0 ){
    iteratorA.remove();
    }else{
    iteratorB.remove();
    }
    }

    }

    // and if i create a class like this:

    public class MyNewSet implements Iterable{
    private Set<class1> set = new HashSet<class1>();
    private List<class1> list = new ArrayList<class1>();

    private DoubleIterator<class1> di = new DoubleIterator<class1>(set, list);

    public MyNewSet(){
    // ...
    }

    public boolean add(class1 c){
    // ...
    }

    ...... some other methods.

    public Iterator<class1> iterator(){
    return di;
    }

    }


    This wouldn't work if I use it to loop through a instance of "MyClass" as ConcurrentModificationException will be thrown.


    Thank you.
    Last edited by philwei; 10-04-2011 at 10:01 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Multi-iterators

    Why not just combine the Collections into a new Collection, then return the iterator of that? For instance, create a new ArrayList, add all of both collections, and return the iterator of said List.

  3. #3
    philwei is offline Member
    Join Date
    Oct 2011
    Location
    Calgary, Canada
    Posts
    4
    Rep Power
    0

    Default Re: Multi-iterators

    Quote Originally Posted by doWhile View Post
    Why not just combine the Collections into a new Collection, then return the iterator of that? For instance, create a new ArrayList, add all of both collections, and return the iterator of said List.
    Because I wanna keep these two collection aware of each other. If one collection adds up, the other one should be removing elements from the end.

  4. #4
    philwei is offline Member
    Join Date
    Oct 2011
    Location
    Calgary, Canada
    Posts
    4
    Rep Power
    0

    Default Re: Multi-iterators

    Quote Originally Posted by doWhile View Post
    Why not just combine the Collections into a new Collection, then return the iterator of that? For instance, create a new ArrayList, add all of both collections, and return the iterator of said List.
    Actually thats a good point. I can create a new ArrayList and fill it up everytime I can iterator() method.

  5. #5
    philwei is offline Member
    Join Date
    Oct 2011
    Location
    Calgary, Canada
    Posts
    4
    Rep Power
    0

    Default Re: Multi-iterators

    Thank you. I didn't think of it before.

Similar Threads

  1. Iterators
    By Archer in forum New To Java
    Replies: 5
    Last Post: 04-09-2011, 12:13 PM
  2. generics and iterators
    By TopNFalvors in forum New To Java
    Replies: 3
    Last Post: 03-29-2011, 05:18 PM
  3. Understanding Iterators
    By Domo230 in forum New To Java
    Replies: 2
    Last Post: 02-12-2011, 12:03 AM
  4. Iterator over Iterators
    By chawlakunal in forum New To Java
    Replies: 2
    Last Post: 05-22-2010, 09:16 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •