Java ArrayList spliterator() Method Last Updated : 15 Dec, 2024 Comments Improve Suggest changes Like Article Like Report The spliterator() method in Java is used to iterate over the elements of an ArrayList. It provides a more efficient way to traverse the collection specially with parallel processing. This method is part of Java 8 and later.Example 1: Here, we will use the spliterator() method to loop through elements in the ArrayList. Java // loop through elements in an // ArrayList using spliterator() import java.util.ArrayList; import java.util.Spliterator; public class GFG { public static void main(String[] args) { // Create an ArrayList ArrayList<String> al = new ArrayList<>(); al.add("Apple"); al.add("Mango"); al.add("Banana"); al.add("Grapes"); // Get Spliterator Spliterator<String> si = al.spliterator(); // Loop through elements si.forEachRemaining(System.out::println); } } OutputApple Mango Banana Grapes Explanation: In the above example, we have used the forEachRemaining() method of Spliterator to sequentially loop through all elements in an ArrayList.Syntax of spliterator() Methodpublic Spliterator<E> spliterator()Return Value: This method returns a Spliterator over the elements in ArrayList.Points to Remember: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 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 + IteratorIt uses tryAdvance() method to iterate elements individually in multiple threads to support Parallel Processing,The forEachRemaining() method to iterate elements sequentially in a single thread,The trySplit() method is used to divide itself into sub-Spliterators to support Parallel Processing.Example 2: Here, we will use the spliterator() method to perform Parallel Processing. Java // Parallel Processing with splititerator() import java.util.ArrayList; import java.util.Spliterator; public class GFG { public static void main(String[] args) { // Create an ArrayList and add numbers ArrayList<Integer> n = new ArrayList<>(); for (int i = 1; i <= 5; i++) { n.add(i); } // Get a Spliterator and split it // for parallel processing Spliterator<Integer> si = n.spliterator(); Spliterator<Integer> si1 = si.trySplit(); // Parallel processing si1.forEachRemaining(num -> System.out.println("Thread 1: " + num)); si.forEachRemaining(num -> System.out.println("Thread 2: " + num)); } } OutputThread 1: 1 Thread 1: 2 Thread 2: 3 Thread 2: 4 Thread 2: 5 Explanation: The above example demonstrates parallel processing using Spliterator by splitting an ArrayList into two parts. The first Spliterator processes one part in "Thread 1," and the second processes the remaining part in "Thread 2."Note: The actual output might vary depending on the JVM, as the division of elements between threads is not guaranteed to be exactly the same every time. Comment More infoAdvertise with us Next Article Java ArrayList spliterator() Method A AmanSingh2210 Follow Improve Article Tags : Java Java-Collections Java - util package java-basics Java-Functions Java-ArrayList +2 More Practice Tags : JavaJava-Collections Similar Reads ArrayList in Java Java ArrayList is a part of the collections framework and it is a class of java.util package. It provides us with dynamic-sized arrays in Java. The main advantage of ArrayList is that, unlike normal arrays, we don't need to mention the size when creating ArrayList. It automatically adjusts its capac 9 min read ArrayList and LinkedList remove() methods in Java with Examples List interface in Java (which is implemented by ArrayList and LinkedList) provides two versions of remove method. boolean remove(Object obj) : It accepts object to be removed. It returns true if it finds and removes the element. It returns false if the element to be removed is not present. Removes t 3 min read ArrayList get(index) Method in Java with Examples The get(index) method of ArrayList in Java is used to retrieve the element at the specified index within the list.Example 1: Here, we will use the get() method to retrieve an element at a specific index in an ArrayList of integers.Java// Java program to demonstrate the working of // get() method in 2 min read Java ArrayList add() Method with Examples The add() method in the ArrayList class is used to add elements to the list. There are different versions of this method. Example 1: In this example, we will use the add() method to add elements at the end of the list.Java// Java Program to demonstrate Addition of // Elements to an ArrayList import 2 min read Java ArrayList addall() Method with Example The addAll() method in the ArrayList class is used to add all the elements from a specified collection into an ArrayList. This method is especially useful for combining collections or inserting multiple elements at once.Example: Here, we will add elements to the end of the list.Java// Java program t 2 min read ArrayList clear() Method in Java with Examples The clear() method in the ArrayList class in Java is used to remove all elements from an ArrayList. After calling this method, the list becomes empty.Example 1: Here, we will use the clear() method to clear elements from an ArrayList of Integers.Java// Java program to demonstrate clear() method // i 2 min read clone() Method - Object Cloning in Java Object cloning in Java refers to creating an exact copy of an object. The clone() method in Java is used to clone an object. It creates a new instance of the class of the current object and initializes all its fields with exactly the contents of the corresponding fields of this object.Example 1: Her 5 min read Arraylist.contains() Method in Java In Java, the ArrayList contains() method is used to check if the specified element exists in an ArrayList or not. Example: Here, we will use the contains() method to check if the ArrayList of Strings contains a specific element.Java// Java program to demonstrate the use of contains() // method with 2 min read ArrayList ensureCapacity() Method in Java with Examples In Java, the ArrayList.ensureCapacity() method is used to increase the internal capacity of an ArrayList to ensure that it can hold a specified minimum number of elements. It helps to optimize performance by reducing the number of dynamic reallocations when adding a large number of elements.Example 2 min read ArrayList forEach() Method in Java In Java, the ArrayList.forEach() method is used to iterate over each element of an ArrayList and perform certain operations for each element in ArrayList.Example 1: Here, we will use the forEach() method to print all elements of an ArrayList of Strings.Java// Java program to demonstrate the use of f 2 min read Like