Java Vector - spliterator() Method
The java.util.Vector.spliterator() method is used to create a late-binding and fail-fast spliterator over the elements in the list.
Syntax
public Spliterator<E> spliterator()
Here, E is the type of element maintained by the container.
Parameters
No parameter is required.
Return Value
Returns a spliterator over the elements in the list.
Exception
NA.
Example:
In the example below, the java.util.Vector.spliterator() method is used to create a spliterator over the elements in the given vector.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a vector Vector<Integer> Vec = new Vector<Integer>(); //populating vector Vec.add(10); Vec.add(20); Vec.add(30); Vec.add(40); Vec.add(50); //creating spliterator object on Vec Spliterator<Integer> splitr = Vec.spliterator(); //display content of the vector using //tryAdvance method System.out.print("The vector contains: "); while(splitr.tryAdvance((n) -> System.out.print(n + " "))); } }
The output of the above code will be:
The vector contains: 10 20 30 40 50
Example:
Lets consider another example to learn the concept of spliterator over the elements in the given vector.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a vector Vector<Integer> Vec = new Vector<Integer>(); //populating vector Vec.add(10); Vec.add(20); Vec.add(-30); Vec.add(-40); Vec.add(-50); //creating spliterator object on Vec Spliterator<Integer> splitr = Vec.spliterator(); //printing estimateSize of the vector System.out.println("Estimated size: " + splitr.estimateSize()); //display content of the vector using //forEachRemaining method System.out.print("The vector contains: "); splitr.forEachRemaining((n) -> System.out.print(n + " ")); } }
The output of the above code will be:
Estimated size: 5 The vector contains: 10 20 -30 -40 -50
❮ Java.util - Vector