Showing posts with label Collection custom Iterator. Show all posts
Showing posts with label Collection custom Iterator. Show all posts

Wednesday, December 4

Iterator Design Pattern

Iterator design pattern in java

Lets write an class that has method next() and hasNext() which basically are backbone for iterator design pattern

This Iterator will be iterating over a collection represented by Collection class

Collection class consist of an array of elements that Iterator will be iterating over.

Additionally methods to add and remove elements from array are also available in Collection class

IteratorTestor class consist of all the scenarios where in




  1. First we are iterating over existing list of elements
  2. Then few elements are added to the array and It is again iterated over
  3. Then few elements are removed from the array and then It is again iterated over

Here is the Well compiled code to try this via java code

-=========================================================


public interface Iterator {
   
    public Object next();
    public boolean hasNext();

}


=========================================================


public interface Container {
   
    public Iterator getIterator();

}

=======================================================

This class consist of an inner class MyIterator which basically becomes the part of collection class and
thus it becomes easy for MyIterator class to work on variables of enclosing class. 


import java.lang.reflect.Array;

public class Collection implements Container {

    public
    String[] list = {"element1", "element2", "element3", "element4" };

    public int index;

    @Override
    public Iterator getIterator() {
        // TODO Auto-generated method stub
        return new MyIterator();
    }
   
    public void addElement(Object obj){
        int arrLength=list.length;
        arrLength++;
        String[] newArr =new String[arrLength];
        newArr[arrLength-1]=obj.toString();
        System.arraycopy(list, 0, newArr, 0, list.length);
        list=new String[newArr.length];
        System.arraycopy(newArr, 0, list, 0, newArr.length);


    }
   
    public void removeElement(Object obj){
        int arrLength=list.length;
        arrLength--;
        String[] newArr =new String[arrLength];
        System.arraycopy(list, 0, newArr, 0, list.length-1);
        list=new String[newArr.length];
        System.arraycopy(newArr, 0, list, 0, newArr.length);


    }


    private class MyIterator implements Iterator {

        @Override
        public Object next() {
            // TODO Auto-generated method stub
            if (hasNext()) {
                return list[index++];
            }
            return null;
        }

        @Override
        public boolean hasNext() {
            // TODO Auto-generated method stub
            if (list.length > index) {
                return true;
            }
            return false;
        }

    }

}

============================================================








public class IteratorTester {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Collection repo = new Collection();

        while (repo.getIterator().hasNext()) {
            System.out.println(repo.getIterator().next());
        }

        System.out.println("=================Lets Add Few More Elements========================");

        repo.index=0;
        repo.addElement("NewElemen1");
        repo.addElement("NewElemen2");
        repo.addElement("NewElemen3");
        while (repo.getIterator().hasNext()) {
            System.out.println(repo.getIterator().next());

        }
        System.out.println("===============Lets Remove few elements==========================");

        repo.index=0;
        repo.removeElement("Elemen1");
        repo.removeElement("Elemen2");
        while (repo.getIterator().hasNext()) {
            System.out.println(repo.getIterator().next());

        }


    }
}
===================================================






And this program will print below logs on console :


++++++++++++++++++++++++++++++++++++++++

element1
element2
element3
element4
=================Lets Add Few More Elements========================
element1
element2
element3
element4
NewElemen1
NewElemen2
NewElemen3
===============Lets Remove few elements==========================
element1
element2
element3
element4
NewElemen1

+++++++++++++++++++++++++++++++++++++++