Accessing a Collection

To access, modify or remove any element from any collection we need to first find the element, for which we have to cycle through the elements of the collection. There are three possible ways to cycle through the elements of any collection.

  1. Using Iterator interface
  2. Using ListIterator interface
  3. Using for-each loop

Steps to use an Iterator

  1. Obtain an iterator to the start of the collection by calling the collection's iterator() method.
  2. Set up a loop that makes a call to hasNext() method. Make the loop iterate as long as hasNext() method returns true.
  3. Within the loop, obtain each element by calling next() method.

Accessing elements using Iterator

Iterator Interface is used to traverse a list in forward direction, enabling you to remove or modify the elements of the collection. Each collection classes provide iterator() method to return an iterator.

Methods of Iterator:

MethodDescription
boolean hasNext()Returns true if there are more elements in the collection. Otherwise, returns false.
E next()Returns the next element present in the collection. Throws NoSuchElementException if there is not a next element.
void remove()Removes the current element. Throws IllegalStateException if an attempt is made to call remove() method that is not preceded by a call to next() method.

import java.util.*;
class Test_Iterator
{
  public static void main(String[] args)
  {
    ArrayList< String> ar = new ArrayList< String>();
    ar.add("ab");
    ar.add("bc");
    ar.add("cd");
    ar.add("de");
    Iterator it = ar.iterator();    //Declaring Iterator
    while(it.hasNext())
    {  
       System.out.print(it.next()+" ");
    }
  }
}

ab bc cd de


Accessing elements using ListIterator

ListIterator Interface is used to traverse a list in both forward and backward direction. It is available to only those collections that implements the List Interface.

Methods of ListIterator:

MethodDescription
void add(E obj) Inserts obj into the list in front of the element that will be returned by the next call to next() method.
boolean hasNext()Returns true if there is a next element. Otherwise, returns false.
boolean hasPrevious()Returns true if there is a previous element. Otherwise, returns false.
E next()Returns the next element. A NoSuchElementException is thrown if there is not a next element.
int nextIndex()Returns the index of the next element. If there is not a next element, returns the size of the list.
E previous()Returns the previous element. A NoSuchElementException is thrown if there is not a previous element.
int previousIndex() Returns the index of the previous element. If there is not a previous element, returns -1.
void remove()Removes the current element from the list. An IllegalStateException is thrown if remove() method is called before next() or previous() method is invoked.
void set(E obj) Assigns obj to the current element. This is the element last returned by a call to either next() or previous() method.

import java.util.*;
class Test_Iterator
{
  public static void main(String[] args)
  {
    ArrayList< String> ar = new ArrayList< String>();
    ar.add("ab");
    ar.add("bc");
    ar.add("cd");
    ar.add("de");
    ListIterator litr = ar.listIterator();
    while(litr.hasNext())   //In forward direction
    {
      System.out.print(litr.next()+" ");
    }
    while(litr.hasPrevious())   //In backward direction
    {
      System.out.print(litr.previous()+" ");
    }
  }
}

ab bc cd de de cd bc ab


Using for-each loop

for-each version of for loop can also be used for traversing the elements of a collection. But this can only be used if we don't want to modify the contents of a collection and we don't want any reverse access. for-each loop can cycle through any collection of object that implements Iterable interface.

import java.util.*;
class ForEachDemo
{
  public static void main(String[] args)
  {
    LinkedList< String> ls = new LinkedList< String>();
    ls.add("a");
    ls.add("b");
    ls.add("c");
    ls.add("d");
    for(String str : ls)
    {
      System.out.print(str+" ");
    }
  }
}

a b c d