Why does the Streams API need a hint for generic type in this case?(在这种情况下,为什么 Streams API 需要泛型类型的提示?)
问题描述
以下编译失败:
@NotNull String defaultFormatter(@Nullable Object value) {
if (value instanceof Collection) {
return ((Collection) value).stream()
.map(MyClass::defaultFormatter)
.collect(Collectors.joining(eol));
}
return String.valueOf(value);
}
尤其是使用 javac 编译时,错误会是:
In particular, when compiled with javac, the error would be:
Error:(809, 94) java: incompatible types:
java.lang.Object cannot be converted to
@org.jetbrains.annotations.NotNull java.lang.String
但是下面的编译就好了:
But the following compiles just fine:
@NotNull String defaultFormatter(@Nullable Object value) {
if (value instanceof Collection) {
Stream<String> stream = ((Collection) value).stream()
.map(MyClass::defaultFormatter);
return stream.collect(Collectors.joining(eol));
}
return String.valueOf(value);
}
唯一的区别是我引入了一个额外的变量.请注意,我没有强制转换,所以没有语义变化.
The only difference would be that I introduced an extra variable. Note that I didn't cast, so no semantic change.
谁能解释为什么需要这样做?
Can anybody explain why is this needed?
推荐答案
这个答案的顶部基本上是 Radiodef 在上面的评论中说.我不想盗用这些话,但如果没有事先说明,---
下面的答案实际上是行不通的.
This top part of this answer is basically what Radiodef said in comments above. I'm not wanting to steal those words, but the answer below the ---
doesn't really work without the prior explanation.
正如 Radiodef 所指出的,这在第一种情况下不起作用的原因是因为它使用的是原始类型 Collection
.相反,使用 Collection<?>
,它会起作用:
As pointed out by Radiodef, the reason why this doesn't work in the first case is because it's using a raw type, Collection
. Instead, use Collection<?>
, and it will work:
return ((Collection<?>) value).stream()
.map(MyClass::defaultFormatter)
.collect(Collectors.joining(eol));
它与显式变量一起工作的原因是未经检查的转换.请注意,以下内容会产生未经检查的转换警告:
The reason why it works with the explicit variable is because of unchecked conversion. Note that the following produces an unchecked conversion warning:
Stream<String> stream = ((Collection) value).stream()
.map(MyClass::defaultFormatter);
RHS上表达式的实际类型是Stream
;您可以将其强制转换为 Stream<String>
,如 JLS Sec 5.1.9:
The actual type of the expression on the RHS is Stream
; you're allowed to coerce that to a Stream<String>
, as described in JLS Sec 5.1.9:
存在从原始类或接口类型(第 4.8 节)G
到 G
.
There is an unchecked conversion from the raw class or interface type (§4.8)
G
to any parameterized type of the formG<T1,...,Tn>
.
<小时>
没有变量你不能做同样的事情的原因有点微妙.这个答案更直接地解决了这个问题:当您使用原始类型时,所有泛型都会被删除从类型,而不仅仅是那些与省略的类型直接相关的.
The reason why you can't do the same without the variable is a bit more subtle. This answer addresses the issue more directly: when you use a raw type, all generics are erased from the type, not just ones directly related to the omitted type.
所以,Stream
为 raw 时的 Stream.collect
类型是泛型时类型的擦除:
So, the type of Stream.collect
when the Stream
is raw is the erasure of the type when it is generic:
Stream.collect(Collector super T,A,R> collector)
返回一个R
;R
的擦除是Object
Stream.collect(Collector<? super T,A,R> collector)
returns anR
;- The erasure of
R
isObject
所以 collect
调用的返回类型是 Object
,正如您在此处看到的那样.这不能通过未经检查的转换自动强制转换为 List
,因为它不是 List
.
so the return type of the collect
call is Object
, as you observe here. This can't be automatically coerced to a List<String>
via unchecked conversion because it's not List
.
这篇关于在这种情况下,为什么 Streams API 需要泛型类型的提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在这种情况下,为什么 Streams API 需要泛型类型的提示?
基础教程推荐
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01