Home » Java programming language

Differences between Iterator and ListIterator in Java

Java Iterator vs ListIterator: Here, we are going to learn about the differences between Iterator and ListIterator in java, compare Iterator v/s ListIterator? What are the differences between Iterator and ListIterator in java?
Submitted by Preeti Jain, on July 24, 2019

In java, we will see first how Iterator differs from ListIterator?

Iterator

  • 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

Example:

    Iterator itr = Set.iterator(); 

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

Iterator program:

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]

In java, we will see first how ListIterator differs from Iterator?

ListIterator

  • 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

Example:

    ListIterator litr = List.listIterator(); 

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

ListIterator program:

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]






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.