No. 9 - Iterator for Collection Of Collection


Problem:


Write a Class which is a Collection of Collection and its hasNext() method should return true is any of its collection has any Object which is not yet retrieved and its next() method should return the same Object.
e.g. if collection of collection is {{1,2,4},{3,5},{6,8}} and we have retrieved 1,2,4 then next call to hasNext() should return true and call to next() should return 3.

Solution:


In your class have a collection of collection and maintain two iterators, primary iterator for collection of collection and secondary iterator for internal collections.

Implement your iterator and in implementation of hasNext() method, if secondary iterator is initialized and has next element then return true otherwise retrieve iterator for next element in primary iterator and assign to secondary iterator to check if that collection has next, do it till next element found or primary iterator reaches end. Follow same approach for returning next element.

IteratorCollectionOfCollection.java
package com.meritmix.blog.algo;
import java.util.ArrayList;
import java.util.Iterator;
/**
 * @author Ravi Yadav
 * This is copyright content of MeritMix.com
 * blog address is blog.meritmix.com 
 */
public class IteratorCollectionOfCollection<E> {
 ArrayList<ArrayList<E>> collectionOfCollection = new ArrayList<ArrayList<E>>();
 //populate
 public IteratorCollectionOfCollection() {
 }
 public Iterator<E> iterator() {
  return new MyIterator();
 }
 
 public class MyIterator implements Iterator<E>{
  java.util.Iterator<ArrayList<E>> primaryItr = collectionOfCollection.iterator();
  java.util.Iterator<E> secondaryItr = null;
  private MyIterator() {
  }

  @Override
  public boolean hasNext() {
   if(secondaryItr!=null && secondaryItr.hasNext())
    return true;
   while (primaryItr.hasNext()) {
    secondaryItr = primaryItr.next().iterator();
    if(secondaryItr.hasNext()){
     return true;
    }
   }
   return false;
  }
  @Override
  public E next() {
   if(secondaryItr!=null && secondaryItr.hasNext())
    return secondaryItr.next();
   while (primaryItr.hasNext()) {
    secondaryItr = primaryItr.next().iterator();
    if(secondaryItr.hasNext()){
     return secondaryItr.next();
    }
   }
   return null;
  }
  @Override
  public void remove() {
   if(secondaryItr == null){
    throw new IllegalStateException();
   }
   
   secondaryItr.remove();
   
  }
 }
 
 //OK, lets test it
 public static void main(String[] args) {
  IteratorCollectionOfCollection<Integer> obj= new IteratorCollectionOfCollection<Integer>();

  ArrayList<Integer> a1= new ArrayList<Integer>();
  a1.add(1);
  a1.add(3);
  a1.add(6);
  obj.add(a1);

  ArrayList<Integer> a2= new ArrayList<Integer>();
  a2.add(4);
  a2.add(2);
  a2.add(7);
  obj.add(a2);
  Iterator<Integer> itr= obj.iterator();
  while (itr.hasNext()) {
   System.out.println(itr.next());
   
  }
 }
 /**
  * @param a1
  */
 public void add(ArrayList<E> a1) {
  collectionOfCollection.add(a1);
  
 }
}

No comments:

Post a Comment