Source: https://dzone.com/articles/implementing-a-sliding-window-streamspliterator-in
Let's go through this tutorial on creating a sliding window in Java using streams and spliterators! Trust me — you'll be happy you did.
In this article, we'll take a look at how to implement a custom sliding window Stream/Spliterator in Java. Does the world need another way of implementing a sliding window operation in Java? Probably not, but you do — for your self-development.
Sliding Window
Simply put, the Sliding Window algorithm is a method of traversing data structures by moving a fixed-size window (sublist) over a sequence in fixed steps.
It gets much more intuitive when shown in an example.
If we wanted to traverse a list [1 2 3 4 5] by using the window of the size 3, we'd be merely looking at the following groups:
- [1 2 3]
- [2 3 4]
- [3 4 5]
But, if we wanted to traverse the same list using a window that's bigger than the collection's size, we wouldn't get a single element.
Implementation
To be able to create a custom Stream, we need to implement a custom Spliterator.
In our case, we need to be able to iterate over groups represented by Stream<T> sequences, so we need to implement the
Spliterator
interface and specify the generic type parameter:
Then, it turns out we have a bunch of methods to implement:
We'll also need a few fields for storing buffered elements, the window size parameter, an iterator of the source collection, and a precomputed size estimation (we'll need that later on):
Before we can start implementing interface methods, we need to have an ability to instantiate our tool.
In this case, we'll restrict the visibility of the constructor and expose a public static factory method instead:
Now, let's implement the easy part of the
Spliterator
methods.
In our case, there's no easy way to split the sequence, so when implementing
trySplit()
, we default to values specified in the documentation. Luckily, size can be calculated quite easily:
In
characteristics()
, we specify:-
ORDERED
— Because of the encounter, order matters. -
NONNULL
— This because elements will never be null (although can contain nulls). -
SIZED
— This is due to the fact that size is predictable.
Implementing tryAdvance
And, here comes the crucial part — the method responsible for the actual grouping and iteration.
Firstly, if the window is smaller than 1, then there's nothing to iterate so that we can short-circuit immediately:
And now, to generate the first sublist, we need to start iterating and filling the buffer:
Once the buffer is filled, we can dispatch the complete group and discard the oldest element from the buffer.
Here comes a crucial part — one might be tempted to pass the
buffer.stream()
to the accept()
method, which is a huge mistake. Streams are lazily bound to an underlying collection, which means that if the source changes, the Stream changes as well.
In order to avoid the problem and decouple our groups from the internal buffer representation, we need to snapshot the current state of the buffer before creating each Stream instance. We'll back Stream instances with arrays to make them as lightweight as possible.
Since Java doesn't support generic arrays, we need to do some ugly casting:
...and, voila, we are ready to use it:
For additional practices, you can implement a possibility of specifying a custom step size (now it's implicitly set to 1).
Complete Example
Source
The complete example can be also found on GitHub.
Have a good idea about how to improve it? Feel free to issue a PR!