Difference between Iterator and Spilt Iterator in Java.


Iterator and split iterator both interface are used for iterating over the collection.

Split iterator is introduced in Java 8 for achieving parallelism. It can split the given set of element and can perform operation parallely using different independent threads. It can traverse the elements parallely as well as sequentially manner. There are following important methods in the splitIterator interface −

  • trySplit - It is used for split the given set of elements into multiple pieces.
  • tryAdvance - It is equivalent to the hasNext/ next methods available in Iterator interface
  • getExactSizeIfKnown <> -It is used to get the size of the given set of elements.
Sr. No.KeyIteratorSplit iterator
1
Basic
It can be used to traverse the element of the collection
It can be used with Stream also.
2
Bulk Operation
It can be used to traverse the element one by one only
It can be used to traverse the elements in bulk.
3
Sequential /Parallel
It can traverse the element in sequential manner only
It can traverse the element in sequential  as well as parallel manner.
4.
External /Internal  Iterator
Iterator uses External Iteration to iterate Collections
Spliterator uses Internal Iteration

Example of Spliterator

public class Main {
   public static void main(String args[]) {
      List<Integer> listOfInteger = new ArrayList<>();
      listOfInteger.add(78);
      listOfInteger.add(10);
      listOfInteger.add(20);
      listOfInteger.add(30);

      Spliterator<Integer> s = listOfInteger.spliterator();
      Spliterator<Integer> s1 = s.trySplit();

      s.forEachRemaining(System.out::println);
      System.out.println("Traverse Second Half ");
      s1.forEachRemaining(System.out::println);
   }
}

Updated on: 09-Sep-2020

606 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements