The spliterator() method of CopyOnWriteArrayList returns an spliterator over the elements in this list in proper sequence. There is no need of synchronization while operating on the spliterator.
Syntax:
public Spliterator spliterator()
Parameters: The function does not accept any parameters.
Return Value: The function returns an spliterator over the elements in the list.
Below programs illustrate the above function:
Program 1:
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
CopyOnWriteArrayList<Integer> ArrLis
= new CopyOnWriteArrayList<Integer>();
ArrLis.add( 32 );
ArrLis.add( 67 );
ArrLis.add( 67 );
ArrLis.add( 100 );
System.out.println( "CopyOnWriteArrayList: " + ArrLis);
Spliterator<Integer> numbers = ArrLis.spliterator();
System.out.println( "list of Numbers:" );
numbers.forEachRemaining((n) -> System.out.println(n));
}
}
|
Output:
CopyOnWriteArrayList: [32, 67, 67, 100]
list of Numbers:
32
67
67
100
Program 2:
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
CopyOnWriteArrayList<String> ArrLis
= new CopyOnWriteArrayList<String>();
ArrLis.add( "gopal" );
ArrLis.add( "gfg" );
ArrLis.add( "jgec" );
ArrLis.add( "sudo" );
System.out.println( "CopyOnWriteArrayList: " + ArrLis);
Spliterator<String> numbers = ArrLis.spliterator();
System.out.println( "list of strings:" );
numbers.forEachRemaining((n) -> System.out.println(n));
}
}
|
Output:
CopyOnWriteArrayList: [gopal, gfg, jgec, sudo]
list of strings:
gopal
gfg
jgec
sudo
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArrayList.html#spliterator–
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!