Java Iterator Vs. Listiterator Vs. Spliterator

Iterator in Java

It allows us to iterate through a collection's elements one after the other in a sequential fashion. It is a key component of the Java Collections Framework and works with lists, sets, queues, and maps, among other collection types. Because the iterator is unidirectional, it can only move in one direction.

Features of Iterator

  • Direction: One-way (exclusively forward).
  • Methods: It offers functions like delete() to eliminate the last element retrieved (optional operation), next() to retrieve the next element, and hasNext() to determine if there are any more elements.
  • Iterable collections such as Collection, List, Set, Queue, and Map (via entrySet()) are supported.
  • Iterators are lightweight, easy to use, and mostly intended for read-only tasks.

IteratorExample.java

Output:

Ram
Ravi
Raghu

Explanation

A List<String> is used to represent the list of names in the Iterator example. We use the list's iterator() method to generate an Iterator. The Iterator provides a straightforward method for iteratively going through the list's elements. As long as there are more elements (iterator.hasNext()), the while loop will keep running. We use an iterator.next() to obtain and output each element within the loop. This example shows how to use an iterator to get elements in a collection sequentially.

ListIterator in Java

In Java, a sub-interface of the Iterator that is particularly made for lists is called ListIterator. By offering bidirectional traversal and the capacity to change the list while iterating, it expands the possibilities of the Iterator.

Features of ListIterator

  • Bidirectional: both forward and backward motion.
  • Methods: ListIterator contains hasPrevious() to see whether there are any prior entries, previous() to go backward, and methods like add(), remove(), and set() for list manipulation in addition to the methods offered by Iterator.
  • Accompanied Collections: primarily utilized with List implementations, such as LinkedList and ArrayList.
  • Features: ListIterators are appropriate for situations where list change is necessary during iteration since they handle both read and write operations.

ListIteratorExample.java

Output:

Red
Green
Blue
Blue
Green
Red

Explanation

The ListIterator example focuses on traversing and modifying lists in both directions. A List<String> is what we use to represent our list of colors. Using the listIterator() function of the list, we are able to obtain a ListIterator. The code's first section uses listIterator.hasNext() and listIterator.next() to illustrate forward traversal. Using listIterator.hasPrevious() and listIterator.previous(), backward traversal is demonstrated in the second section. ListIterator is more flexible than a basic Iterator since it has extra methods (add(), remove(), and set()) for altering the list as it is being traversed.

Spliterator in Java

As part of the Stream API, Java 8 brought the Spliterator interface. Large datasets can be processed more effectively in parallel thanks to its capabilities for parallel traversal and element splitting.

Features of Spliterator

  • Supports multiple processing simultaneously.
  • Techniques: Spliterator offers several functions, including tryAdvance() for iteratively going over elements, forEachRemaining() for processing in bulk, trySplit() for splitting the elements into two halves for processing in parallel, and characteristics() for describing its properties.
  • Accepted Collections: Use keySet() and values() to work with Collection, List, Set, and Map collection types.
  • Splitterators can be used with parallel streams for better performance because they are made for splitting and iteration in parallel.

SpliteratorExample.java

Output:

1
2
3
4
5

Explanation

We operate with a list of integers in the Spliterator example, which is represented by a List<Integer>. To get a Spliterator, utilize the spliterator() function. Spliterator is meant to be processed in parallel; sequential and parallel traversal are both demonstrated in the code. For sequential traversal, use the forEachRemaining() method; for parallel processing, break the Spliterator into two pieces using trySplit(). This example shows how to use Spliterator in the context of a list in a basic way. Spliterator is very helpful when working with large datasets and concurrent streams.

Differnece between Iterator, Listiterator, and Spliterator

FeatureIteratorListIteratorSpliterator
DirectionsBidirectionalSupports parallel processing
MethodshasNext(), next(), remove()hasNext(), next(), hasPrevious(), previous(), add(), remove(), set()tryAdvance(), forEachRemaining(), trySplit(), characteristics()
Supported CollectionsCollection, List, Set, Queue, Map (via entrySet())List (ArrayList, LinkedList)Collection, List, Set, Map (via keySet(), values())
CharacteristicsSimple, lightweight, read-onlySupports both read and write operationsSupports parallel iteration and splitting





Latest Courses