In this post, We will talk and learn about Java Spliterator Using an example
Java Spliterator interface is an example of an internal iterator that breaks down the stream into the smaller parts and these smaller parts can be processed in parallel.
Features Of Spliterator :
The following are the list of features provided by Spliterator in Java.
- Spliterator was introduced in Java 8
- It provides support for parallel processing of the stream of elements for the collection framework.
- It provides tryAdvance()method to iterate elements individually by different threads. It helps in parallel processing.
- We may use to iterate elements sequentially in a single Thread, use forEachRemaining()
- The method trySplit() is used partition the spliterator, if it is possible.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.infotech.client; import java.util.Arrays; import java.util.List; import java.util.Spliterator; /** * Java Spliterator Example * @author KK Tutorials */ public class SpliteratorTest { public static void main(String[] args) { List<String> wordList = Arrays.asList("Apple", "Banana", "Orange","Guava","Mango","Grapes"); //Getting spliterator1 and spliterator2 from Collection Spliterator<String> spliterator1 = wordList.spliterator(); Spliterator<String> spliterator2 = spliterator1.trySplit(); System.out.println("Traversing the first half of the spliterator.."); spliterator1.forEachRemaining(System.out::println); System.out.println("Traversing the second half of the spliterator.."); spliterator2.forEachRemaining(System.out::println); } } |
Output of this Program:
Traversing the first half of the spliterator..
Guava
Mango
Grapes
Traversing the second half of the spliterator..
Apple
Banana
Orange
You May Also Like:
What are new Features added in Java 8?
When we choose LongAdder and LongAccumulator over AtomicLong & AtomicInteger ?
How can we maintain Immutability of a class with a mutable reference ?
What is BlockingQueue? How can we implement Producer and Consumer problem using Blocking Queue?
How to get number of available processors in Java ?
That’s all about Java Spliterator Example?
If you have any feedback or suggestion please feel free to drop in below comment box.