The spliterator() method of ArrayList returns a Spliterator of the same elements as ArrayList but created Spliterator is late-binding and fail-fast. A late-binding Spliterator binds to the source of elements. It means that Arraylist at the point of the first traversal, first split, or the first query for estimated size, rather than at the time the Spliterator is created. It can be used with Streams in Java 8. Also, it can traverse elements individually and in bulk too. Spliterator is a better way to traverse over element because it provides more control over elements.
Spliterator = Splitting + Iterator
- It uses tryAdvance() method to iterate elements individually in multiple Threads to support Parallel Processing,
- forEachRemaining() method to iterate elements sequentially in a single Thread,
- trySplit() method to divide itself into Sub-Spliterators to support Parallel Processing.
Spliterator supports both Sequential and Parallel processing of data.
If you observe the output of below program’s output, you will find Spliterator.forEachRemaining() method works in the same way as ArrayList.foreach() but it provides better performance.
public Spliterator<E> spliterator()
Returns: This method returns a Spliterator over the elements in ArrayList.
Below programs illustrate spliterator() method of ArrayList:
Example 1: To demonstrate spliterator() method on ArrayList which contains a list of emails.
list of Emails: firstname.lastname@example.org email@example.com firstname.lastname@example.org email@example.com firstname.lastname@example.org
Example 2: To demonstrate spliterator() method on ArrayList which contains list of Users.
list of Emails: User name : Aman and user age: 24 User name : Suraj and user age: 23 User name : Amar and user age: 24 User name : Kajal and user age: 22
- CopyOnWriteArraySet spliterator() method in Java
- PriorityQueue spliterator() method in Java
- CopyOnWriteArrayList spliterator() method in Java
- LinkedTransferQueue spliterator() method in Java
- HashSet spliterator() method in Java
- PriorityBlockingQueue spliterator() method in Java
- LinkedList spliterator() method in Java
- ArrayDeque spliterator() method in Java
- ArrayBlockingQueue spliterator() method in Java
- ConcurrentSkipListSet spliterator() method in Java
- LinkedBlockingDeque spliterator() method in Java
- ConcurrentLinkedQueue spliterator() method in Java
- Java 8 | LinkedBlockingQueue spliterator() method with Examples
- ConcurrentLinkedDeque Spliterator() method in Java with Examples
- Java.util.ArrayList.addall() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : bishnoisunil53