Signup/Sign In

Java Arrays spliterator() Method

In this tutorial, we will learn about spliterator() method of the Arrays class in Java. This is an extension of splitting and iterators in Java with some more features. Let's see its syntax and examples to understand the concept.

Syntax

static Spliterator.OfInt spliterator(int[] array)

List of Overloading Methods of spliterator() Method

This table contains all the overloaded variants of spliterator() method.

Method Description

static Spliterator.OfDouble spliterator(double[] array)

This method returns a Spliterator.OfDouble covering all of the specified array.

static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive)

This method returns a Spliterator.OfDouble covering the specified range of the specified array.

static Spliterator.OfInt spliterator(int[] array)

This method returns a Spliterator.OfInt covering all of the specified array.

static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive)

This method returns a Spliterator.OfInt covering the specified range of the specified array.

static Spliterator.OfLong spliterator(long[] array)

This method returns a Spliterator.OfLong covering all of the specified array.

static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive)

This method returns a Spliterator.OfLong covering the specified range of the specified array.

static <T> Spliterator<T> spliterator(T[] array)

This method returns a Spliterator covering all of the specified array.

static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive)

This method returns a Spliterator covering the specified range of the specified array.

enlightenedSpliterator = Splitting + Iterator

Advantages of Spliterator

  • It supports Parallel Programming functionality.
  • It supports both Sequential and Parallel Processing of data.
  • It provides better performance.

Example of spliterator() method on list object of class

In the example given below, we have an ArrayList class Student. By using spliterator method we are splitting it and then we can easily print it using forEachRemaining method.

import java.util.ArrayList;
import java.util.Spliterator;
class Student { 
	int rollno; 
	String name;
	public Student(int rollno, String name) 
	{ 
		this.rollno = rollno; 
		this.name = name; 
	}  
} 
class StudyTonight{ 
	public static void main(String args[]) 
	{  
		ArrayList<Student> list = new ArrayList<Student>(); 
		list.add(new Student(1, "AAA"));
		list.add(new Student(2, "BBB"));
		list.add(new Student(3, "CCC"));
		list.add(new Student(4, "DDD"));
		list.add(new Student(5, "EEE"));		
		Spliterator<Student> students = list.spliterator(); 
		System.out.println("list of Students:"); 
		students.forEachRemaining((n) -> System.out.println(n.rollno+" "+n.name)); 
	}
}


list of Students:
1 AAA
2 BBB
3 CCC
4 DDD
5 EEE

Example: Overloading Methods of spliterator()

In the following example, we implemented spliterator() on all the supported datatyes.

import java.util.Arrays;
import java.util.Spliterator;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		int[] array1 = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
        Spliterator<Integer> s1 = Arrays.spliterator(array1);
        s1.forEachRemaining((n) -> System.out.print(n+" "));     
        
        System.out.print("\n");   
        
        double[] array2 = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
        Spliterator<Double> s2 = Arrays.spliterator(array2);
        s2.forEachRemaining((n) -> System.out.print(n+" "));  
        
        System.out.print("\n");   
        
        long[] array3 = {121, 41, 32, 14, 33, 71, 99, 32};
        Spliterator<Long> s3 = Arrays.spliterator(array3);
        s3.forEachRemaining((n) -> System.out.print(n+" "));                 
	}
}


2 4 6 8 10 12 14 16 18 20
2.0 4.0 6.0 8.0 10.0 12.0 14.0 16.0 18.0 20.0
121 41 32 14 33 71 99 32

Conclusion:

In this tutorial, we learned about spliterator() method of Arrays class in Java. This method is a combination of splitting and iterator. According to official documentation an object for traversing and partitioning elements of a source. The source of elements covered by a Spliterator could be, for example, an array, a Collection, an IO channel, or a generator function.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.