Iterator in Java

Overview

In this article, we will learn about the different cursors in Java.

Java has three types of cursor available currently.

  1. Enumeration (Java 1.0v)
  2. Iterator (Java 1.2v)
  3. ListIterator (Java 1.2v)
  4. Spliterator (Java 1.8v)
1.) Enumeration

Enumeration Interface came in Java 1.0v, It is a legacy cursor and can be used to get objects one by one from legacy classes like Vector.

Enumeration interface is deprecated, and it is not recommended to use now.

Methods of Enumeration
boolean hasMoreElements();
E nextElement();
Example
Vector vector = new Vector();
 vector.add(1);
 vector.add(2);
 vector.add(3);
Enumeration en =  vector.elements();
while(en.hasMoreElements())
System.out.println(en.nextElement());
Output

1

2

3

 

Limitations of Enumeration
  1. It is only available for legacy classes.
  2. By using Enumeration we can perform only read operation. Creation, Updation, and Deletion of objects is not allowed.
2.) Iterator

It is a universal cursor. We can use it with any collection object. By using the Iterator, we can perform read and remove operation.

Methods of Iterator
boolean hasNext()
E next()
void remove();
void forEachRemaining(Consumer<? super E> action)

forEachRemaining(Consumer<? super E> action) is added in Java 1.8v.

Example
package com.geekscoder.collections;

import java.util.ArrayList;
import java.util.Iterator;
 
public class IteratorExample {
 
  public static void main(String args[]){
    ArrayList languages = new ArrayList();
    names.add("Java");
    names.add("C++");
    names.add("Python");
 
    Iterator it = names.iterator();
 
    while(it.hasNext()) {
      String obj = (String)it.next();
      System.out.println(obj);
    }
  }
 
Output:

Java

C++

Python

Limitation of Iterator
  1. It can only move in forwarding direction.
  2. the addition operation is not allowed.
3.) ListIterator

ListIterator is an Interface which extends the Iterator Interface. By using ListIterator we can either move to a forward or backward direction hence It is a bidirectional cursor. 

Using ListIterator we can perform replacement and addition of new objects including of read and remove operation.

Methods of ListIterator
boolean hasNext()
E next()
int nextIterator()
boolean hasPrevious()
E previous()
int previousIndex()
void remove()
void set(E e)
void add(E e)
Example
package com.geekscoder.collections;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
 
public class ListIteratorExample {
  public static void main(String a[]){
    ListIterator listIterator = null;
    List lang = new ArrayList();
    lang.add("Java");
    lang.add("Python");
    lang.add("Groovy");

    listIterator = lang.listIterator();
 
    System.out.println("Iterating the list in forward direction");
    while(listIterator.hasNext()){
       System.out.println(listIterator.next());
    }
    System.out.println("Iterating the list in backward direction");
    while(listIterator.hasPrevious()){
       System.out.println(listIterator.previous());
    }
  }
}
Limitations of ListIterator

ListIterator is the most powerful cursor but its limitation is it is applicable only for the classes which implement the List interface. Hence it is not a universal cursor.

4.) Spliterator

Spliterator is an interface in Java Collection API. It was introduced in Java 1.8v in java.utill package to support Parallel Programming functionality. It can be used with both Collection API and Stream API.

Important Methods
boolean tryAdvance(Consumer<? super T> action)
void forEachRemaining(Consumer<? super T> action)
Spliterator<T> trySplit()
long estimateSize()
long getExactSizeIfKnown()
int characteristics()
boolean hasCharacteristics(int characteristics)
Comparator<? super T> getComparator()
Example
package com.geekscoder.collections;
import java.util.Spliterator;
import java.util.ArrayList;
import java.util.List;

public class SpliteratorExample
{
  public static void main(String[] args) 
  {
	List<String> names = new ArrayList<>();
	names.add("Java");
	names.add("Python");
	names.add("Groovy");
		
	// Getting Spliterator
	Spliterator<String> namesSpliterator = names.spliterator();
		
	// Traversing elements
	namesSpliterator.forEachRemaining(System.out::println);			
   }
}
Output:

Java

Python

Groovy

That’s all about cursors in Java.