HTML Java

Java Iterator


Iterator

Iterator is an interface and it has various methods

  • public Object next()
  • public Boolean hasNext()
  • public void remove()

Implementing class of Iterator is not fixed. Implementation of iterator different 2 collections provides. We can’t take iterator object direct, to take iterator object whenever on any collection suppose we have list in the above program.

Iterator i = list.iterator();
when we call this method then we get the Object of implementing class of List.

Looks how will get an object of that class?

Class ArrayList implements List {

class Itr implements Iterator {
overrides all the method of Iterator here
}

public Iterator iterator(){
return new Itr();
}
}

interface List extends Collection{
public abstract Iterator iterator();
}

interface Collection{
public abstract Iterator iterator();
}

interface Iterator{}

Iterator is an interface we can’t create object of Iterator. And implementation of this interface is done by different 2collection by accordingly like arraylist, vector and so on. And implementing class object of this interface is responsible to travers in forward direction only.



ArrayList

  • ArrayList is a class which implements list and list internally implement collection so when we call iterator method on arrayList class has method.
  • So inside this method 1 class its implement Iterator interface and all the method is overridden in inside this class and with Iterator iterator() we are return new Itr().
  • The object of Itr because its implements Iterator and we can hold the implementing class object into Iterator interface reference variable.

Iterator

  • Iterator interface internally traverse the element in forward direction.
  • For traversal iterator pointer uses the default position of iterator which is -1.
  • And to move that pointer in forward direction hasNext() method is used.
  • next() method return current position of pointing that element, hasNext method checks the arraylist has elements or not.
  • If there then return true or false and remove method remove the elements.
  • And it can be used in all the collections.

ListIterator

  • This interface internally inherits the Iterator and the implementation of ListIterator is not provided by all the collection.
  • The implementation of ListIterator is provided by only List Interface because it contains ListIteraotr method.
  • ListIterator is method as well as interaface.
  • public abstract ListIterator listIterator() this method is inside the list interface why this is not in collection interface because they don’t want that every collection can traverse in both the direction.
  • ListIteraotor can be used by the implementing class of List Interface.

ListIterator implementing class LinkdeList, ArrayList, Stack, Vector, Iterator can be used to traverse all the collection.

Example

package com.collection.programs;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class IteartorAndListIterator {

public static void main(String[] args) {
// TODO Auto-generated method stub
List list = new ArrayList();
list.add(10);
list.add(1);
list.add(12);
list.add(4);
list.add(8);
System.out.println(list);

// Iterator traversal
Iterator itr = list.iterator();
System.out.print("Iterator of List : ");
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
// we can typecast here
// Integer it =(Integer)itr.next();
}
System.out.println();
// listIterator Traversal
ListIterator listItr = list.listIterator();
System.out.println("move in the forward direction");
while (listItr.hasNext()) {

System.out.print(listItr.next() + " ");

}
System.out.println();
System.out.println("move in the backward direction");

while (listItr.hasPrevious()) {

System.out.print(listItr.previous() + " ");
}
System.out.println();
System.out.println("iterate list using for loop");
for(int i =0;i < list.size();i++) {
System.out.print(list.get(i)+" ");
}

System.out.println();
System.out.println("iterate list using for each loop");
for(Object o:list) {
System.out.print(o+" ");
}
}
}
Try it »

When you are using ListIterator then first you have to traverse in forward direction then go in backward direction otherwise you will get null value because by default iterator will be -1 and we call previous method then how it will go when it’s already in -1 position that’s why we first traverse the list in forward direction.



Vector

  • It’s a concrete class which is providing the implementation of list interface.
  • An object of this class is responsible to creating dynamic resizable array in memory it’s similar to array list but it’s a legacy collection and it’s synchronized.
  • It is an implementing class of List Interface whose 1 object is also responsible to creating dynamic resizable array into the memory like arraylist.
  • Initial capacity of vector is 10. Vector is a legacy collection legacy means from when java is there.
  • ArrayList is introduced in: jdk 1.2. When we add beyond its capacity then it increase the double of actual size.

In vector we can traverse the element in forward direction with iterator as well as with enumeration. Vector is work on insertion order.


Commonly used constructor in Vector

  • public Vector()
  • public Vector(int capacity)
  • public Vector(Collection c)
  • public Vector(int capacity,int increment)


Difference between ArrayList and Vector

    ArrayList
  • It’s a Part of Collection Framework and its introduce in jdk1.2. In this collection you cannot give incremental factor.
  • By default it’s a 50%. ArrayList is not synchronized.
    Vector
  • It’s in beginning in java it’s a legacy Collection. In vector you can give incremental factor.
  • When we create an object of vector then inside heap capacity of 10 array is created and when we add beyond this and we didn’t provide any incremental factor then it increase double.
  • By default 2 times. Vector is synchronized. Most of the method of Vector is synchronized.