How does Spliterator work in Java 8?

15

In Java 8 (which was released in March 2014), there is a new interface called Spliterator . It has a similar purpose to the Iterator , but it is designed to perform iterations in parallel .

However, even after reading a lot about this interface and even having seen some sample code, I still can not figure out how to use it. As Java 8 was not released yet (edit: when I asked this question in 2013 it had not yet been released), there is currently very little material on the internet about it. Once I have some structure I want to iterate in parallel (for example: a search tree), how do I effectively get a parallel iteration using the Spliterator?

    
asked by anonymous 16.12.2013 / 05:30

1 answer

15

Spliterator looks like a class created for other more sophisticated classes to use. The basic logic is in trySplit() , which tries to divide the remaining elements into two Spliterator : current and a new one, which the method returns.

For example:

void <T> showSize(Spliterator<T> sit) {
  System.out.println("Estimated size for sit: "+sit.getExactSizeIfKnown());
  Spliterator<T> sit2 = sit.trySplit();
  System.out.println("New estimated size for sit: "+sit.getExactSizeIfKnown());
  System.out.println("Estimated size for sit2: "+sit2.getExactSizeIfKnown());
  return;
}

The method receives a Spliterator , sit , prints the size (this may not work), then splits it in two with trySplit() , and prints the new sizes.

Assuming that the sizes can be printed accurately, there are two possibilities about what will be observed:

  • An error of type NullPointerException when calling a method in sit2 , because it was not possible to split Spliterator ;
  • sit and sit2 , in addition, has the size that sit had originally.
  • I personally do not recommend trying to use Spliterator directly, since optimizing this type of thing is difficult - so even Spliterator has things like collection type, estimated size, sequential processing of the rest, fact that Spliterator is not thread safe, etc.

    If you want parallelism, use parallel Stream . It's very easy to use, and all optimization logic is already built in.

        
    16.12.2013 / 07:33