Summary of Content: Take a closer look at Java iterator spliterator from the previous article.
Portal: Java iterator spliteratormp.weixin.qq.com/s/lE7a1oP4j…
In the previous article, we briefly illustrated the use of spliterators in Java 8 using int arrays. There are a few issues that we haven’t addressed in the previous article:
-
What spliterator implementations are used by the Arrays class
-
What are the types of Consumer in the implementation above
-
Whether the collection provides a spliterator operation
As shown in the figure above, the Spliterator function of Arrays provides four parameter types corresponding to different parameters, and four underlying implementations.
When the parameter type is T, the underlying implementation is ArraySpliterator. If the parameter type is int, the underlying implementation is IntArraySpliterator. When the argument type is long, the underlying implementation is LongArraySpliterator. If the argument type is double, the underlying implementation is DoubleArraySpliterator.
The diagram above shows the inheritance structure of the four underlying implementation classes.
public interface OfPrimitive<T, T_CONS, T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>>
extends Spliterator<T> {
@Override
T_SPLITR trySplit();
@SuppressWarnings("overloads")
boolean tryAdvance(T_CONS action);
@SuppressWarnings("overloads")
default void forEachRemaining(T_CONS action) {
do { } while (tryAdvance(action));
}
}
Copy the code
OfPrimitive only declares the trySplit and tryAdvance methods and provides the default implementation of the forEachRemaining method.
ArraySpliterator is special. It stores arrays of type Object and implements the Spliterator interface directly. The Spliterator interface subclasses trySplit, tryAdvance, and estimateSize() compared to spliterator.ofprimitive.
The main difference between Spliterator and spliterator.ofprimitive is that Spliterator is used to split iterated Object arrays. The Spliterator.OfPrimitive interface is for iterating arrays of ints, longs, and doubles.
Arrays provides spliterator functions of the parameter types described in 4. The implementation principle is the same, but the Consumer types passed by tryAdvance and forEachRemaining are different. Java 8 provides four types of consumers corresponding to the different Spliterators of Arrays 4:
-
Consumer
-
IntConsumer
-
LongConsumer
-
DoubleConsumer
The data type for Consumer is Object.
IntConsumer, LongConsumer, and DoubleConsumer for int, Long, and Double, respectively.
Each of the four Consumer interfaces declares the Accept method and provides a default framework implementation of andThen.
public interface Iterable<T> {
Iterator<T> iterator();
/ * *
* @ since 1.8
* /
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
/ * *
* @ since 1.8
* /
default Spliterator<T> spliterator() {
return Spliterators.spliteratorUnknownSize(iterator(), 0);
}
}
Copy the code
The Iterable interface is the top-level interface of the collection class. Since Java 8, Spliterator and forEach methods have been added to Iterable. Therefore, all implementations of Iterable can use spliterator to split iterations.
Because all primitive types of data stored inside the collection are automatically boxed, the argument to the forEach method is to receive a Consumer of type Object.
–END–
Identify the QR code, follow us