Java Collection spliterator() with Examples Last Updated : 27 Nov, 2023 Comments Improve Suggest changes Like Article Like Report The spliterator() in Java Collection creates a spliterator over the elements in the collection. In simple words, it's an iterator that allows you to traverse the elements individually but is specially designed to work in parallel processing scenarios. The difference between Iterator and Spliterator is iterator does not support parallel programming but spliterator supports parallel programming. Syntaxpublic Spliterator<E> spliterator();Here E denotes the type of element stored inside the collection. Return ValueIt returns a spliterator across elements in the collection. Example 1In this example, we have used some of the methods of spliterator that we need to understand before we proceed to the example. spliterator(): It returns a spliterator across the element of the calling collection.tryAdvance(): It returns a boolean value. It returns true, if there are more elements exist else it returns false. Java import java.util.List; import java.util.ArrayList; import java.util.Spliterator; import java.io.*; class GFG { public static void main (String[] args) { List<String> list1 = new ArrayList<String>(); list1.add("Pen"); list1.add("Paper"); list1.add("Rubber"); list1.add("Pencil"); Spliterator<String> spliterator = list1.spliterator(); System.out.println("The list contains:"); while(spliterator.tryAdvance((element)->System.out.print(element+" "))); } } Output: The list contains:Pen Paper Rubber PencilExample 2In this example, we have used spliterator() in Hashset Collection.The same way as above example it traverses through the set and prints the element. Java import java.util.HashSet; import java.util.Spliterator; import java.io.*; class GFG { public static void main (String[] args) { HashSet<Integer> list1 = new HashSet<Integer>(); list1.add(1); list1.add(2); list1.add(3); list1.add(4); list1.add(5); list1.add(6); Spliterator<Integer> spliterator1 = list1.spliterator(); System.out.println("The collection contains :"); while(spliterator1.tryAdvance((element)->System.out.print(element+" "))); } } Ouput: The collection contains :1 2 3 4 5 6 Comment More infoAdvertise with us Next Article Java Collection spliterator() with Examples S saswatdas121 Follow Improve Article Tags : Java Java-Collections Java-Collections-Class Practice Tags : JavaJava-Collections Similar Reads Collectors toSet() in Java with Examples Collectors toSet() returns a Collector that accumulates the input elements into a new Set. There are no guarantees on the type, mutability, serializability, or thread-safety of the Set returned. This is an unordered Collector i.e, the collection operation does not commit to preserving the encounter 2 min read AbstractCollection in Java with Examples The AbstractCollection class in Java is a part of the Java Collection Framework and implements the Collection interface. It is used to implement an unmodifiable collection, for which one needs to only extend this AbstractCollection Class and implement only the iterator and the size methods. Class Hi 3 min read Collectors toList() method in Java with Examples The toList() method of Collectors Class is a static (class) method. It returns a Collector Interface that gathers the input data onto a new list. This method never guarantees type, mutability, serializability, or thread-safety of the returned list but for more control toCollection(Supplier) method c 2 min read AbsractCollection iterator() Method in Java with Examples The iterator() method of Java AbstractCollection is used to return an iterator of the same elements as that of the Collection. The elements are returned in random order from what was present in the Collection. Syntax: Iterator iterate_value = AbstractCollection.iterator(); Parameters: The function d 2 min read Java Swing | JSeparator with examples JSeparator is a part of Java Swing framework. It is used to create a dividing line between two components. More specifically, it is mainly used to create dividing lines between menu items in a JMenu. In JMenu or JPopupMenu addSeparartor function can also be used to create a separator. Constructor of 4 min read Like