Java - Difference Between Iterator and ListIterator

Java | Iterator Vs. ListIterator: In this tutorial, we will learn about the Iterator and ListIterator in Java and the differences between Iterator and ListIterator. By Preeti Jain Last updated : March 30, 2024

What is an Iterator in Java

  • Iterator is an interface which is introduced in java.
  • Iterator is unidirectional by nature (i.e It iterates or traverse an element or object in one direction only that's is forward direction)
  • While iterating an element by Iterator we can perform read and remove operation and we can't perform any other operation like add an object, replace an object.
  • We can create an Iterator object by using iterator() method of Iterator interface.

Syntax

Interface_name object_name = Collection_method. Iterator_method

Iterator can be used in List, Set, Queue, etc and it supports other interfaces of collection framework too alone with List.

Example of an Iterator in Java

import java.util.*; 

class IteratorClass {
 public static void main(String[] args) {
  // Creating a Set interface object
  Set set = new HashSet();

  // By using add() method to add few elements in set
  set.add("Java");
  set.add("Python");
  set.add("C++");
  set.add("Ruby");
  set.add("C");


  // Creating an object of Iterator interface
  Iterator itr = set.iterator();

  // loop for traversing an element
  while (itr.hasNext()) {
   String str = (String) itr.next();
   if (str.equals("C"))
    itr.remove();
  }
  // Display elements of Set interface 
  System.out.println("The final list of Iterator :" + set);
 }
}

Output

E:\Programs>javac IteratorClass.java

E:\Programs>java IteratorClass
The final list of Iterator :[Ruby, Python, C++, Java]

What is a ListIterator in Java

  • ListIterator is a child interface of Iterator which is introduced in java.
  • ListIterator is a bi-directional by nature (i.e It iterates or traverse an element or object in both directions that's is the forward direction and backward direction).
  • While iterating an element by ListIterator we can perform read, remove, replace and add operation and we can perform any other operation.
  • We can create a ListIterator object by using listIterator() method of ListIterator interface.

Syntax

Interface_name(or class_name) object_name = Collection_method.ListIterator_method

ListIterator can be used in List only and it does not support other interfaces of collection framework like Set, Queue, etc.

Example of ListIterator in Java

import java.util.*;

class ListIteratorClass {
 public static void main(String[] args) {
  // Creating a LinkedList object
  LinkedList ll = new LinkedList();

  // By using add() method to add few elements in linked list
  ll.add("Java");
  ll.add("Python");
  ll.add("C++");
  ll.add("Ruby");
  ll.add("C");

  // Creating an object of ListIterator interface
  ListIterator litr = ll.listIterator();

  // loop for traversing an element
  while (litr.hasNext()) {
   String str = (String) litr.next();

   // We are removing an element by using remove() method
   if (str.equals("C"))
    litr.remove();

   // We are replacing an element by using set() method
   if (str.equals("C++"))
    litr.set("TOC");

   // We are adding an element by using add() method
   if (str.equals("C"))
    litr.add("Data Structure");
  }

  // Display elements of Set interface 
  System.out.println("The final list of ListIterator :" + ll);
 }
}

Output

E:\Programs>javac ListIteratorClass.java

E:\Programs>java ListIteratorClass
The final list of ListIterator :[Java, Python, TOC, Ruby, Data Structure]

Difference Between Iterator and ListIterator in Java

The following table shows differences between an Iterator and a ListIterator in Java:

Sr. No. Iterator ListIterator
1. An Iterator is a part of the java.util package. A ListIterator is also part of the java.util package.
2. An Iterator can iterate over any collection (List, Set, Map). A ListIterator is primarily used for List collections.
3. An Iterator supports only forward iteration. A ListIterator supports both forward and backward iteration.
4. An Iterator provides methods like next() and hasNext() for iteration. A ListIterator offers additional methods like previous() and hasPrevious() for bidirectional traversal.
5. An Iterator doesn't support positioning within the collection. A ListIterator supports positioning at any point in the list.
6. An Iterator cannot modify the underlying collection while iterating. A ListIterator allows adding, removing, and replacing elements during iteration.
7. An Iterator is implemented by collections to provide a way to traverse elements. A ListIterator is implemented by List interface for traversing List elements.
8. An Iterator is suitable for iterating over any type of collection in a forward direction. A ListIterator is an ideal for scenarios where you need to traverse a List bidirectionally and modify it while traversing.
9. N/A A ListIterator provides methods like add(), remove(), set() which are specific to Lists.
10. An Iterator doesn't support backward traversal. A ListIterator allows backward traversal using previous() method.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.