不兼容的通用通配符捕获
在以下代码片段中:
package test;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
public class WildcardsTest<K, V> {
private Iterator<Map.Entry<K, ? extends Collection<V>>> iterator;
public WildcardsTest(Map<K, ? extends Collection<V>> map) {
iterator = map.entrySet().iterator();
/* Type mismatch: cannot convert from
Iterator<Map.Entry<K,capture#1-of ? extends Collection<V>>> to
Iterator<Map.Entry<K,? extends Collection<V>>> */
}
}
尽管类型似乎完全匹配,但分配不正确。
我设计了一个肮脏的解决方法,将 Collection 的类型指定为另一个泛型参数,如下所示
public class WildcardsTest<K, V, C extends Collection<V>> {
private Iterator<Map.Entry<K, C>> iterator;
public WildcardsTest(Map<K, C> map) {
iterator = map.entrySet().iterator();
}
}
:有什么方法可以在保持类型安全的同时摆脱它吗?
谢谢。
in the following snippet:
package test;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
public class WildcardsTest<K, V> {
private Iterator<Map.Entry<K, ? extends Collection<V>>> iterator;
public WildcardsTest(Map<K, ? extends Collection<V>> map) {
iterator = map.entrySet().iterator();
/* Type mismatch: cannot convert from
Iterator<Map.Entry<K,capture#1-of ? extends Collection<V>>> to
Iterator<Map.Entry<K,? extends Collection<V>>> */
}
}
The assignment is incorrect, although the types seem to match exactly.
I've devised a dirty workaround by specifying the type of the Collection as another generic parameter, like this:
public class WildcardsTest<K, V, C extends Collection<V>> {
private Iterator<Map.Entry<K, C>> iterator;
public WildcardsTest(Map<K, C> map) {
iterator = map.entrySet().iterator();
}
}
But that C
parameter is really a "don't care" type that only complicates the API, is there any way to get rid of it while maintaining type safety?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样做,它会工作:
您仍然可以像这样使用迭代器:
作为参考,请阅读 get 和 put 原则(最初来自 Java 泛型和集合)
Do it like this and it will work:
You can still use the iterator like this:
For reference, read the get and put principle (originally from Java Generics and Collections)
两个
?
通配符可以绑定到两个不同的类。这样说,很明显存在类型不匹配:当您引入
C
时,您“强迫它们”引用同一个类。The two
?
-wildcards can be bound to two different classes. Put it this way, it is quite obvious that there is a type mismatch:When you introduce the
C
, you "force them" to refer to the same class.