Close

Java - Spliterator Examples

Java 

Using Spliterator#getComparator() to get underlying source Comparator instance. This method will return null if no Comparator has been set. It will throw IllegalStateException if the source does not have SORTED characteristics.

package com.logicbig.example;

import java.util.SortedSet;
import java.util.Spliterator;
import java.util.TreeSet;

public class SpliteratorComparatorExample {
public static void main (String[] args) {
SortedSet<Test> set =
new TreeSet<>((o1, o2) ->
o1.str.compareTo(o2.str));

set.add(new Test("two"));
set.add(new Test("one"));
Spliterator<Test> s = set.spliterator();
System.out.println(s.getComparator());
System.out.println(set);
}

private static class Test {
private final String str;

private Test (String str) {
this.str = str;
}

@Override
public String toString () {
return "Test{str='" + str + "'}";
}
}
}

Output

com.logicbig.example.SpliteratorComparatorExample$$Lambda$1/1585509557@7b90559e
[Test{str='one'}, Test{str='two'}]
Original Post




Using Spliterator#estimateSize() and Spliterator#getExactSizeIfKnown(). These methods return the size of the remaining elements to traverse.

package com.logicbig.example;

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

public class SpliteratorSizeExample {

public static void main (String[] args) {
List<String> list = Arrays.asList("Apple", "Banana", "Orange");
Spliterator<String> s = list.spliterator();
s.tryAdvance(System.out::println);
System.out.println(s.estimateSize());
System.out.println(s.getExactSizeIfKnown());
}
}

Output

Apple
2
2
Original Post




Using Spliterators to create a Spliterator instance for an array.

package com.logicbig.example;

import java.util.Spliterator;
import java.util.Spliterators;

public class SpliteratorsExample {

public static void main (String[] args) {
Spliterator<String> s = Spliterators.spliterator(
new String[]{"one", "two"}, 0);
s.forEachRemaining(System.out::println);
}
}

Output

one
two
Original Post




Using Spliterator for primitive arrays.

package com.logicbig.example;

import java.util.Arrays;
import java.util.Spliterator;
import java.util.function.IntConsumer;

public class SpliteratorPrimitiveExample {
public static void main (String[] args) {
int[] ints = {3, 4, 6, 7};
Spliterator.OfInt s = Arrays.spliterator(ints);
s.forEachRemaining((IntConsumer) System.out::println);
}
}

Output

3
4
6
7
Original Post

Using Spliterator#trySplit() to partition the spliterator. It's used for parallel processing, specifically by Fork/Join framework.

package com.logicbig.example;

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

public class SpliteratorTrySplitExample {
public static void main (String[] args) {
List<String> list = Arrays.asList("Apple", "Banana", "Orange");

Spliterator<String> s = list.spliterator();
Spliterator<String> s1 = s.trySplit();

s.forEachRemaining(System.out::println);
System.out.println("-- traversing the other half of the spliterator --- ");
s1.forEachRemaining(System.out::println);
}
}

Output

Banana
Orange
-- traversing the other half of the spliterator ---
Apple
Original Post




Querying all characteristics of this Spliterator.

package com.logicbig.example;

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

public class SpliteratorCharacteristics {
public static void main (String[] args) {
List<String> list = new ArrayList<>();

Spliterator<String> s = list.spliterator();

if (s.hasCharacteristics(Spliterator.ORDERED)) {
System.out.println("ORDERED");
}
if (s.hasCharacteristics(Spliterator.DISTINCT)) {
System.out.println("DISTINCT");
}
if (s.hasCharacteristics(Spliterator.SORTED)) {
System.out.println("SORTED");
}
if (s.hasCharacteristics(Spliterator.SIZED)) {
System.out.println("SIZED");
}

if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
System.out.println("CONCURRENT");
}
if (s.hasCharacteristics(Spliterator.IMMUTABLE)) {
System.out.println("IMMUTABLE");
}
if (s.hasCharacteristics(Spliterator.NONNULL)) {
System.out.println("NONNULL");
}
if (s.hasCharacteristics(Spliterator.SUBSIZED)) {
System.out.println("SUBSIZED");
}
}
}

Output

ORDERED
SIZED
SUBSIZED
Original Post




Finding spliterator characteristic and printing elements of the List

package com.logicbig.example;

import java.util.Arrays;
import java.util.List;

public class SpliteratorExample {
public static void main (String[] args) {
List<String> list = Arrays.asList("Apple", "Banana", "Orange");

System.out.println(list.spliterator().characteristics());
for (String s : list) {
System.out.println(s);
}
}
}

Output

16464
Apple
Banana
Orange
Original Post

Using Spliterator#tryAdvance() and Spliterator#forEachRemaining() to traverse the elements. With tryAdvance() method, we traverse elements individually one by one, whereas, forEachRemaining() gives us remaining elements in bulk.

package com.logicbig.example;

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

public class SpliteratorTryAdvanceExample {
public static void main (String[] args) {
List<String> list = Arrays.asList("Apple", "Banana", "Orange");

Spliterator<String> s = list.spliterator();
s.tryAdvance(System.out::println);
System.out.println(" --- bulk traversal");
s.forEachRemaining(System.out::println);

System.out.println(" --- attempting tryAdvance again");
boolean b = s.tryAdvance(System.out::println);
System.out.println("Element exists: " + b);

}
}

Output

Apple
--- bulk traversal
Banana
Orange
--- attempting tryAdvance again
Element exists: false
Original Post




See Also