Consider the following code fragment
String strings[] = {\"test\"};
final List collect = java.util.Arrays.stream(strings).collect(java.util.str
The signature of the method Stream::toArray
looks as follows. Please note that the type parameters T
and A
are completely unrelated.
public interface Stream<T> {
<A> A[] toArray(IntFunction<A[]> generator);
}
In the source of ReferencePipeline.java, you can find the following comment:
Since
A
has no relation toU
(not possible to declare thatA
is an upper bound ofU
) there will be no static type checking. Therefore use a raw type and assumeA == U
rather than propagating the separation ofA
andU
throughout the code-base. The runtime type ofU
is never checked for equality with the component type of the runtime type ofA[]
. Runtime checking will be performed when an element is stored inA[]
, thus ifA
is not a super type ofU
anArrayStoreException
will be thrown.