Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, Iterator and ListIterator are interfaces used for iterating over collections. They provide different functionalities and are used in various scenarios. Here’s a detailed explanation of both, along with examples:

'Iterator'

The Iterator interface provides methods to iterate over any collection (e.g., ArrayList, HashSet). It allows only forward traversal and is not limited to lists.

Key Methods:

  1. hasNext(): Returns true if the iteration has more elements.
  2. next(): Returns the next element in the iteration.
  3. remove(): Removes the last element returned by the iterator (optional operation).

Example:

				
					import java.util.ArrayList;
import java.util.Iterator;

public class IteratorExample {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");

        // Getting the Iterator
        Iterator<String> iterator = list.iterator();

        // Traversing elements using Iterator
        while (iterator.hasNext()) {
            String element = iterator.next();
            System.out.println(element);
            // Removing an element using Iterator
            if (element.equals("C")) {
                iterator.remove();
            }
        }

        // Displaying the list after removal
        System.out.println("List after removal: " + list);
    }
}

				
			

'ListIterator'

The ‘ListIterator‘ interface extends ‘Iterator‘ and provides additional methods to iterate over lists. It allows bidirectional traversal (both forward and backward) and supports more operations.

Key Methods:

  1. hasNext(): Returns true if the list iterator has more elements when traversing the list in the forward direction.
  2. next(): Returns the next element in the list and advances the cursor position.
  3. hasPrevious(): Returns true if the list iterator has more elements when traversing the list in the reverse direction.
  4. previous(): Returns the previous element in the list and moves the cursor position backwards.
  5. add(E e): Inserts the specified element into the list (optional operation).
  6. set(E e): Replaces the last element returned by next() or previous() with the specified element (optional operation).
  7. remove(): Removes the last element returned by next() or previous() (optional operation).

Example:

				
					import java.util.ArrayList;
import java.util.ListIterator;

public class ListIteratorExample {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");

        // Getting the ListIterator
        ListIterator<String> listIterator = list.listIterator();

        // Traversing elements forward using ListIterator
        System.out.println("Forward Traversal:");
        while (listIterator.hasNext()) {
            String element = listIterator.next();
            System.out.println(element);
            // Adding an element using ListIterator
            if (element.equals("B")) {
                listIterator.add("E");
            }
        }

        // Traversing elements backward using ListIterator
        System.out.println("Backward Traversal:");
        while (listIterator.hasPrevious()) {
            String element = listIterator.previous();
            System.out.println(element);
            // Replacing an element using ListIterator
            if (element.equals("C")) {
                listIterator.set("F");
            }
        }

        // Displaying the list after modifications
        System.out.println("List after modifications: " + list);
    }
}

				
			

'Differences' Between 'Iterator' and ListIterator

1. Direction:

  • Iterator‘ allows only forward traversal.
  • ListIterator‘ allows both forward and backward traversal.

2. Operations:

  • Iterator‘ supports basic operations like ‘hasNext()', ‘next()‘, and ‘remove()‘.
  • ListIterator‘ supports additional operations like ‘hasPrevious()‘, ‘previous()‘, ‘add(E e)‘, and ‘set(E e)‘.

3. Applicability:

  • Iterator‘ can be used with any collection that implements the ‘Iterable‘ interface.
  • ListIterator‘ can be used only with lists (‘List‘ interface).

These interfaces are essential for navigating and manipulating collections in Java, offering different levels of control and functionality.

Scroll to Top