Why is `Stream.collect` type-safe and `Stream.toArray(IntFunctionlt;A[]gt;)` is not?(为什么 `Stream.collect` 是类型安全的,而 `Stream.toArray(IntFunctionlt;A[]gt;)` 不是?)
问题描述
考虑以下代码片段
String strings[] = {"test"};
final List<String> collect = java.util.Arrays.stream(strings).collect(java.util.stream.Collectors.toList());
final Double[] array = java.util.Arrays.stream(strings).toArray(Double[]::new);
为什么 Java 可以在 collect-case 中保证正确的类型(将 collect 的泛型类型更改为例如 Double 会导致编译时错误),但不能在 array case 中保证正确的类型(编译正常,尽管 apply(int)
of Double[]::new
给出一个 Double[]
,而不是 Object[]
,而是抛出 ArrayStoreException
如果如上使用不正确)?
Why can Java guarantee the correct type in the collect-case (changing the generic type of collect to e.g. Double leads to a compile time error), but not in the array case (compiles fine, despite apply(int)
of Double[]::new
gives a Double[]
, not an Object[]
, but throws ArrayStoreException
if used incorrectly as above)?
如果我更改流的类型而不更改 toArray
调用中给定的 IntFunction
,那么生成编译时错误的最佳方法是什么?p>
What would be the best way to generate a compile time error in case I change the type of the stream without changing the given IntFunction
in the toArray
call?
推荐答案
方法 Stream::toArray
的签名如下所示.请注意类型参数T
和A
是完全不相关的.
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);
}
在ReferencePipeline.java,可以找到如下注释:
In the source of ReferencePipeline.java, you can find the following comment:
由于 A
与 U
没有关系(不可能声明 A
是 U
)不会有静态类型检查.因此使用原始类型并假设 A == U
而不是传播 A
和 U
的分离在整个代码库中.U
的运行时类型永远不会检查与 A[]
运行时类型的组件类型是否相等.当元素存储在 A[]
中时将执行运行时检查,因此如果 A
不是U
的超类型会抛出 ArrayStoreException
.
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.
这篇关于为什么 `Stream.collect` 是类型安全的,而 `Stream.toArray(IntFunction<A[]>)` 不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 `Stream.collect` 是类型安全的,而 `Stream.toArray(IntFunction<A[]>)` 不是?


基础教程推荐
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13