Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Difference between an Iterator and ListIterator in Java
Next article icon

Difference between an Iterator and ListIterator in Java

Last Updated : 21 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Iterators are used in Collection framework in Java to retrieve elements one by one. It can be applied to any Collection object. By using Iterator, we can perform both read and remove operations. Iterator must be used whenever we want to enumerate elements in all Collection framework implemented interfaces like Set, List, Queue, Deque and also in all implemented classes of Map interface. Iterator is the only cursor available for entire collection framework. Iterator object can be created by calling iterator() method present in Collection interface.

// Here "c" is any Collection object. itr is of // type Iterator interface and refers to "c" Iterator itr = c.iterator();

ListIterator It is only applicable for List collection implemented classes like arraylist, linkedlist etc. It provides bi-directional iteration. ListIterator must be used when we want to enumerate elements of List. This cursor has more functionality(methods) than iterator. ListIterator object can be created by calling listIterator() method present in List interface.

// Here "l" is any List object, ltr is of type // ListIterator interface and refers to "l" ListIterator ltr = l.listIterator();

Differences between Iterator and ListIterator:

  1. Iterator can traverse only in forward direction whereas ListIterator traverses both in forward and backward directions. Example: 
Java
import java.io.*; import java.util.*;  class IteratorDemo1 {     public static void main(String[] args)     {         ArrayList<Integer> list             = new ArrayList<Integer>();          list.add(1);         list.add(2);         list.add(3);         list.add(4);         list.add(5);          // Iterator         Iterator itr = list.iterator();          System.out.println("Iterator:");         System.out.println("Forward traversal: ");          while (itr.hasNext())             System.out.print(itr.next() + " ");          System.out.println();          // ListIterator         ListIterator i = list.listIterator();          System.out.println("ListIterator:");         System.out.println("Forward Traversal : ");          while (i.hasNext())             System.out.print(i.next() + " ");          System.out.println();          System.out.println("Backward Traversal : ");          while (i.hasPrevious())             System.out.print(i.previous() + " ");          System.out.println();     } } 
Output:
Iterator: Forward traversal:  1 2 3 4 5   ListIterator: Forward Traversal :  1 2 3 4 5  Backward Traversal :  5 4 3 2 1
  1. ListIterator can help to replace an element whereas Iterator cannot. Example: 
Java
import java.util.ArrayList; import java.util.ListIterator;  public class ListIteratorDemo2 {     public static void main(String[] args)     {          ArrayList<Integer> aList             = new ArrayList<Integer>();         aList.add(1);         aList.add(2);         aList.add(3);         aList.add(4);         aList.add(5);          System.out.println("Elements of ArrayList: ");         for (Integer i : aList) {             System.out.println(i);         }         ListIterator<Integer> l             = aList.listIterator();         l.next();         l.set(80000);          System.out.println("\nNow the ArrayList"                            + " elements are: ");         for (Integer i : aList) {             System.out.println(i);         }     } } 
Output:
Elements of ArrayList:  1 2 3 4 5  Now the ArrayList elements are:  80000 2 3 4 5
  1. OUTPUT Table showing Difference between Iterator and ListIterator
IteratorListIterator
Can traverse elements present in Collection only in the forward direction.Can traverse elements present in Collection both in forward and backward directions.
Helps to traverse Map, List and Set.Can only traverse List and not the other two.
Indexes cannot be obtained by using Iterator.It has methods like nextIndex() and previousIndex() to obtain indexes of elements at any time while traversing List.
Cannot modify or replace elements present in CollectionWe can modify or replace elements with the help of set(E e)
Cannot add elements and it throws ConcurrentModificationException.Can easily add elements to a collection at any time.
Certain methods of Iterator are next(), remove() and hasNext().Certain methods of ListIterator are next(), previous(), hasNext(), hasPrevious(), add(E e).

Next Article
Difference between an Iterator and ListIterator in Java

K

kartikgoel1999
Improve
Article Tags :
  • Java
  • Difference Between
  • Java Programs
  • Java-Iterator
Practice Tags :
  • Java

Similar Reads

    Replace an Element From ArrayList using Java ListIterator
    To replace an element from an ArrayList, the set() method of ListIterator interface can be used. set() method of ListIterator replaces the last element which is returned by the next() or previous() methods, along with the given element. Two ways of replacing the elements using ListIterator shown bel
    3 min read
    Iterate a LinkedList in Reverse Order in Java
    For traversing a linked list in reverse order we can use Descending Iterator or List Iterator 1. Descending Iterator Syntax: LinkedList<String> linkedlist = new LinkedList<>(); Iterator<String> listIterator = linkedlist.descendingIterator(); Returns: Descending Iterator returns the
    2 min read
    Java Program to Add an Element to ArrayList using ListIterator
    In this article, we will learn how to add an element to ArrayList using ListIterator. The ListIterator is used to return a list iterator over the list elements. The listIterator() returns an iterator over the list from the beginning, but the listIterator(index) returns an iterator over the list from
    2 min read
    Converting LinkedHashSet to Different Collection Types in Java
    In Java, LinkedHashSet is a Class in the Java Collections Framework that extends the Collection HashSet and maintains the insertion order of the elements. The underlined data structure of the LinkedHastSet is that the insertion order is preserved. Converting LinkedHashSet to Other CollectionsIn cert
    4 min read
    Traverse Through ArrayList in Forward Direction in Java
    ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java. The listIterator() method of java.util.ArrayList class is used to return a list iterator over the elements in this list (in a proper organized sequence). ArrayList can be
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences