Iterator vs Collection in Java


Iterator

It is used in Collection Framework so as to retrieve elements as and when they are required.

public interface Iterator

It can be used with the ‘next’ function to move and access the next elements. The ‘remove’ function can be used to remove an element from the data structure.

It is quicker in comparison to Collections, since the number of operations associated with Iterator is less.

Below is an example of an iterator working with a list −

Example

 Live Demo

mport java.io.*;
import java.util.*;
public class Demo{
   public static void main(String[] args){
      ArrayList<String> my_list = new ArrayList<String>();
      my_list.add("Its");
      my_list.add("a");
      my_list.add("sample");
      Iterator iterator = my_list.iterator();
      System.out.println("The list contains the following elements : ");
      while (iterator.hasNext())
      System.out.print(iterator.next() + ",");
      System.out.println();
   }
}

Output

The list contains the following elements :
Its,a,sample,

A class named Demo contains the main function. Here, a new array list is defined and elements are added to it using the ‘add’ function. An iterator is defined and it is iterated over the array list and elements are iterated over one by one, and then printed on the console.

Collection

public interface Collection<E> extends Iterable<E>

Here, E refers to the type of elements that will be returned. The Collection Framework is used to define different classes and interfaces that will be used to represent a group of objects as a single entity.

Collection can use the ‘add’ function, the ‘iterate’ function, ‘remove’ function and ‘clear’ function to add element, iterate through the elements one by one, remove elements or clear the entire structure respectively.

Let us see an example −

Example

 Live Demo

import java.io.*;
import java.util.*;
public class Demo{
   public static void main (String[] args){
      int my_arr[] = new int[] {56, 78, 90};
      Vector<Integer> my_vect = new Vector();
      Hashtable<Integer, String> my_hashtab = new Hashtable();
      my_vect.addElement(0);
      my_vect.addElement(100);
      my_hashtab.put(0,"sample");
      my_hashtab.put(100,"only");
      System.out.print("The first element in the array is ");
      System.out.println(my_arr[0]);
      System.out.print("The first element in the vector is ");
      System.out.println(my_vect.elementAt(0));
      System.out.print("The first element in the hashtable is ");
      System.out.println(my_hashtab.get(0));
   }
}

Output

The first element in the array is 56
The first element in the vector is 0
The first element in the hashtable is sample

A class named Demo contains the main function. Here, a new integer array is defined and elements are added to it. Now a new Vector is defined, as well as a Hashtable. Elements are added to vector using the ‘addElement’ function. Elements are added to Hashtable using the ‘put’ function. The elements of all the three structures are printed on the console.

Updated on: 04-Jul-2020

263 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements