How does Spliterator work in Java 8?

Asked

Viewed 1,315 times

16

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

However, even after reading a lot about this interface and even seeing some sample codes, I still can’t understand how to use it. Since Java 8 has not even been released yet (Dit: 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 Spliterator?

1 answer

16


The Spliterator looks more like a class created for other more sophisticated classes to use. The basic logic is in trySplit(), attempting to divide the remaining elements in two Spliterator: the 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), so split it in two with the trySplit(), and prints the new sizes.

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

  1. A mistake of the kind NullPointerException when calling a method in sit2, because it was not possible to divide the Spliterator;
  2. sit and sit2, combined, has the size that sit had originally.

I personally do not recommend trying to use the Spliterator directly, because optimizing this kind of thing is difficult -- so even the Spliterator has things like collection type, estimated size, sequential processing of the remainder, the fact of the Spliterator not be thread-safe, etc.

If you want parallelism, use the Stream parallel. It is very easy to use, and all optimization logic is already embedded.

  • Daniel, you know what criteria the trySplit() method uses to make that division?

  • 1

    @Geisonsantos Depends on the implementation. On something like a ArrayList, he probably splits it in half.

  • Sometimes you have to access one Spliterator, how to do parallel processing from a Iterable: https://answall.com/a/345434/64969; in practice, however, I only saw it as a means to call StreamSupport, no major changes related to this. Let’s say that, although I have accessed, I did not actually use the Spliterator in response, and it remains a black box for me

Browser other questions tagged

You are not signed in. Login or sign up in order to post.