Write a Java program to traverse the Vector collection using spliterator() method


The code you provided is written in Java and demonstrates how to use a Spliterator to traverse and print the elements of a Vector.

The Vector class is a part of the Java Collections Framework and provides an implementation of a dynamic array. In this code, a Vector named vec_list is created and populated with integer values.

A Spliterator is obtained from the vec_list using the spliterator() method. The Spliterator interface was introduced in Java 8 and is used for traversing and partitioning elements of a source in a parallel manner.

The forEachRemaining() method of the Spliterator interface is called on the items object. It takes a lambda expression as an argument, which is used to define the action to be performed on each element of the Vector. In this case, the lambda expression (n) -> System.out.println(n) prints each element of the Vector on a new line.

Source Code

import java.util.*;
public class Traverse
{
	public static void main(String[] args)
	{
		Vector < Integer > vec_list = new Vector < Integer > ();
		vec_list.add(10);
		vec_list.add(20);
		vec_list.add(30);
		vec_list.add(40);
		vec_list.add(50);
 
		Spliterator < Integer > items = vec_list.spliterator();
 
		System.out.println("Vector Elements : ");
		items.forEachRemaining((n) -> System.out.println(n));
	}
}

Output

Vector Elements :
10
20
30
40
50

Example Programs