Java ArrayList spliterator() Method



Description

The Java ArrayList spliterator() method creates a late-binding and fail-fast Spliterator for the elements in this arrayList. Late binding means the spliterator binds to the source of elements at the time of traversal and not when it is created. Spliterator is a better iterator and provides more controls over items during traversal.

Declaration

Following is the declaration for java.util.ArrayList.spliterator() method

public Spliterator<E> spliterator()

Parameters

NA

Return Value

This method returns a Spliterator over the elements in this list in proper sequence.

Exception

NA

Getting Spliterator from an ArrayList of Integers Example

The following example shows the usage of Java ArrayList spliterator() method. We're creating a ArrayList of Integers. We're adding couple of Integers to the ArrayList object using add() method calls per element and using spliterator() method, we're iterating the list and print all the elements.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Spliterator;

public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list 
      ArrayList<Integer> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(0);
      arrayList.add(1);
      arrayList.add(2);
      arrayList.add(3);
      arrayList.add(4);
      arrayList.add(5);
      arrayList.add(6);
	  
      Spliterator<Integer> spliterator = arrayList.spliterator();
      spliterator.forEachRemaining(i -> System.out.println(i));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

0
1
2
3
4
5
6

Getting Spliterator from an ArrayList of Strings Example

The following example shows the usage of Java ArrayList Spliterator() method. We're creating a ArrayList of Strings. We're adding couple of Strings to the ArrayList object using add() method calls per element and using Spliterator() method, we're iterating the list and print all the elements.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Spliterator;

public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      ArrayList<String> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add("A");
      arrayList.add("B");
      arrayList.add("C");
      arrayList.add("D");
      arrayList.add("E");
      arrayList.add("F");
	  
      Spliterator<String> spliterator = arrayList.spliterator();
      spliterator.forEachRemaining(i -> System.out.println(i));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

A
B
C
D
E
F

Getting Spliterator from an ArrayList of Objects Example

The following example shows the usage of Java ArrayList spliterator() method. We're creating a ArrayList of Student objects. We're adding couple of Student objects to the ArrayList object using add() method calls per element and using spliterator() method, we're iterating the list and print all the elements.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Spliterator;

public class ArrayListDemo {
   public static void main(String[] args) {

      // create an empty arrayList
      ArrayList<Student> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(new Student(1, "Julie"));
      arrayList.add(new Student(2, "Robert"));
      arrayList.add(new Student(3, "Adam"));

      Spliterator<Student> spliterator = arrayList.spliterator();
      spliterator.forEachRemaining(i -> System.out.println(i));    
   }
}

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

[ 1, Julie ]
[ 2, Robert ]
[ 3, Adam ]
java_util_arraylist.htm
Advertisements