Javatpoint Logo
Javatpoint Logo

Java Interface Spliterator

Spliterators can be used for traversing the elements of a source one by one. These sources could be an array, a Collection, an IO Channel or a generator function.

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 Interface Spliterator Declaration

Java Interface Spliterator Methods

The list of all Java Spliterator methods with some useful description are given below-

SN Modifier & Type Method Description
1) int characteristics() It is used to get a set of characteristics of this Spliterator and its elements.
2) long estimateSize() It is used to get an estimate of the number of elements remaining to iterate or returns Long.MAX_VALUE if infinite, unknown, or too expensive to compute.
3) default void ForEachRemaining(Consumer action>) It is used to performs the given action for each remaining element sequentially in the current thread until all elements have been processed or the action throws an exception.
4) default comparator getComaparator() It is used to get a Comparator if the given Spliterator?s source is SORTED by a Comparator.
5) default long getExactSizeIfKnown() It is used to get an estimateSize() of the SIZED spliterator, otherwise returns -1.
6) default boolean hasCharacteristics(int characteristics) It returns true if this Spliterator's characteristics() contain all of the given characteristics.
7) boolean TryAdvance(Consumer action>) It is used to get the existing elements upon performing the specified action on it.
8) splierator trySplit() It is used to splits the invoking spliterator if this spliterator can be partitioned, returns a reference to a new spliterator for the partition. Otherwise, it returns null.

Example 1

Test it Now

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

Example 2

Test it Now

Output:

List of Fruit name-
Mango
Banana
Apple

Example 3

Test it Now

Output:

Python
C++
Traversing the next half of the spliterator-
Java
Android



Help Others, Please Share

facebook twitter pinterest