Difference Between Iterator and Spliterator in Java
Last Updated :
15 Oct, 2020
The Java Iterator interface represents an object capable of iterating through a collection of Java objects, one object at a time. The Iterator interface is one of the oldest mechanisms in Java for iterating collections of objects (although not the oldest — Enumerator predated Iterator).
Moreover, an iterator differs from the enumerations in two ways:
1. Iterator permits the caller to remove the given elements from the specified collection during the iteration of the elements.
2. Method names have been enhanced.
Java
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class JavaIteratorExample1 {
public static void main(String[] args)
{
List<String> list = new LinkedList<>();
list.add( "Welcome" );
list.add( "to" );
list.add( "our" );
list.add( "website" );
System.out.println( "The list is given as : "
+ list);
Iterator<String> itr = list.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
itr.remove();
System.out.println(
"After the remove() method is called : "
+ list);
}
}
|
Output
The list is given as : [Welcome, to, our, website]
Welcome
to
our
website
After the remove() method is called : [Welcome, to, our]
Like Iterator and ListIterator, Spliterator is a Java Iterator, which is used to iterate elements one-by-one from a List implemented object.
The main functionalities of Spliterator are:
- Splitting the source data
- Processing the source data
The Interface Spliterator is included in JDK 8 for taking the advantages of parallelism in addition to sequential traversal. It is designed as a parallel analogue of an iterator.
Java
import java.util.*;
import java.util.stream.Stream;
public class InterfaceSpliteratorExample {
public static void main(String args[])
{
ArrayList<Integer> list = new ArrayList<>();
list.add( 101 );
list.add( 201 );
list.add( 301 );
list.add( 401 );
list.add( 501 );
Stream<Integer> str = list.stream();
Spliterator<Integer> splitr = str.spliterator();
System.out.println( "Estimate size: "
+ splitr.estimateSize());
System.out.println( "Exact size: "
+ splitr.getExactSizeIfKnown());
System.out.println( "Boolean Result: "
+ splitr.hasCharacteristics(
splitr.characteristics()));
System.out.println( "Elements of ArrayList :" );
splitr.forEachRemaining(
(n) -> System.out.println(n));
Stream<Integer> str1 = list.stream();
splitr = str1.spliterator();
Spliterator<Integer> splitr2 = splitr.trySplit();
if (splitr2 != null ) {
System.out.println( "Output from splitr2: " );
splitr2.forEachRemaining(
(n) -> System.out.println(n));
}
System.out.println( "Output from splitr1: " );
splitr.forEachRemaining(
(n) -> System.out.println(n));
}
}
|
Output
Estimate size: 5
Exact size: 5
Boolean Result: true
Elements of ArrayList :
101
201
301
401
501
Output from splitr2:
101
201
Output from splitr1:
301
401
501
Difference between Iterator and Spliterator in java :
Iterator
|
Spliterator
|
Introduced in Java 1.2 |
Introduced in Java 1.8 |
Iterator only iterates elements individually |
Spliterator traverse elements individually as well as in bulk |
It is an iterator for whole collection API |
It is an iterator for both Collection and Stream API, except Map implementation classes |
It uses external iteration |
It uses internal iteration. |
It is a Universal iterator |
It is Not a Universal iterator |
It does not support parallel programming |
It supports parallel programming by splitting the given element set so that each set can be processed individually. |
Share your thoughts in the comments
Please Login to comment...