Java Collections - Arrays.spliterator() Examples

Code Snippets Java Collections Java 


Class:

java.util.Arrays

java.lang.Objectjava.lang.Objectjava.util.Arraysjava.util.ArraysLogicBig

Methods:

These methods return a corresponding Spliterator.

public static <T> Spliterator<T> spliterator(T[] array)
public static <T> Spliterator<T> spliterator(T[] array,
                                             int startInclusive,
                                             int endExclusive)
public static Spliterator.OfInt spliterator(int[] array)
public static Spliterator.OfInt spliterator(int[] array,
                                            int startInclusive,
                                            int endExclusive)
public static Spliterator.OfLong spliterator(long[] array)
public static Spliterator.OfLong spliterator(long[] array,
                                             int startInclusive,
                                             int endExclusive)
public static Spliterator.OfDouble spliterator(double[] array)
public static Spliterator.OfDouble spliterator(double[] array,
                                               int startInclusive,
                                               int endExclusive)

Examples


package com.logicbig.example.arrays;

import java.util.Arrays;
import java.util.Spliterator;

public class SpliteratorExample {

public static void main(String... args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Spliterator<Integer> s1 = Arrays.spliterator(arr);
Spliterator<Integer> s2 = s1.trySplit();
System.out.println("spliterator 1");
s1.forEachRemaining(System.out::println);
System.out.println("spliterator 2");
s2.forEachRemaining(System.out::println);
}
}

Output

spliterator 1
5
6
7
8
9
spliterator 2
1
2
3
4




spliterator(T[] array) example:

package com.logicbig.example.arrays;

import java.util.Arrays;
import java.util.Spliterator;

public class SpliteratorExample2 {

public static void main(String... args) {
String[] arr = {"Apple", "banana", "pie"};
Spliterator<String> s = Arrays.spliterator(arr);
s.tryAdvance(System.out::println);
s.tryAdvance(System.out::println);
}
}

Output

Apple
banana




package com.logicbig.example.arrays;

import java.util.Arrays;
import java.util.Spliterator;

public class SpliteratorExample3 {

public static void main(String... args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
//index based
Spliterator<Integer> s1 = Arrays.spliterator(arr, 0, 4);
Spliterator<Integer> s2 = s1.trySplit();
System.out.println("spliterator 1");
s1.forEachRemaining(System.out::println);
System.out.println("spliterator 2");
s2.forEachRemaining(System.out::println);
}
}

Output

spliterator 1
3
4
spliterator 2
1
2




See Also