简体   繁体   English

Java迭代器返回的结果的通用类型不兼容

[英]Incompatible generic type for result returned by Java iterator

I have an object which is a type of Map, and I am trying to iterate it with iterator. 我有一个对象,它是Map的一种,并且我正在尝试使用迭代器对其进行迭代。 However, I cannot pass the compile, the code are as following. 但是,我无法通过编译,代码如下。

// Returned from other method, and the run time type is Map<String, B>
Map<String, ? extends A> map = ...

// Option 1: cannot pass.
Iterator<Map.Entry<String, ? extends A>> iterator = map.entrySet().iterator();

// Option 2: cannot pass either.
// Where B is a subclass extends from A
Iterator<Map.Entry<String, B>> iterator = map.entrySet().iterator();

Can someone help me out? 有人可以帮我吗? Thanks. 谢谢。

I suggest you use the foreach loop instead of using the Iterator directly 我建议您使用foreach循环,而不是直接使用Iterator

Map<String, ? extends A> map = new HashMap<>();
for (Map.Entry<String, ? extends A> entry : map.entrySet()) {
    String key = entry.getKey();
    A value = entry.getValue();
}

If you really want to store the Iterator in a variable, instead of this 如果您真的想将Iterator存储在变量中,请不要使用此变量

// ERROR
Iterator<Map.Entry<String, ? extends A>> iterator = map.entrySet().iterator();

you have to do this: 您必须这样做:

Iterator<? extends Map.Entry<String, ? extends A>> iterator = map.entrySet().iterator();

The reason for the extra ? extends 额外的原因? extends ? extends is the wildcard nested in in the Map.Entry<String, ? extends A> ? extends是嵌套在Map.Entry<String, ? extends A>的通配符Map.Entry<String, ? extends A> Map.Entry<String, ? extends A> declaration. Map.Entry<String, ? extends A>声明。 If you had a variable of type 如果您有类型的变量

Iterator<Map.Entry<String, ? extends A>>

then in principle each entry could have a value of a different subtype of A, so each Map.Entry would be of a different type. 那么原则上每个条目都可以具有A的不同子类型的值,因此每个Map.Entry都可以具有不同的类型。 By contrast, 相比之下,

Iterator<? extends Map.Entry<String, ? extends A>>

means that each entry is of a single type. 表示每个条目都是单一类型。 (But you don't know what type it is, since it's a wildcard.) (但是您不知道它是什么类型,因为它是通配符。)

In practice, this doesn't matter for Iterator since you're only getting objects of type Map.Entry<String, ? extends A> 实际上,这对于Iterator无关紧要,因为您只获得Map.Entry<String, ? extends A>类型的对象Map.Entry<String, ? extends A> Map.Entry<String, ? extends A> out of it. Map.Entry<String, ? extends A> But if you had some other type that allowed you to put things into it (such as a Set ) then the difference would be significant. 但是,如果您有其他类型可以将内容放入其中(例如Set ),则差异会很大。 For example, 例如,

Set<? extends Map.Entry<String, ? extends A>>

would only allow getting entries from the set, but 只允许从集合中获取条目,但是

Set<Map.Entry<String, ? extends A>>

would allow adding entries to the set, and those entries might contain different subtypes of A, possibly polluting the set. 将允许将条目添加到集合中,并且这些条目可能包含A的不同子类型,从而可能污染集合。

For further discussion on this, see 有关此的进一步讨论,请参见

as well as these Stack Overflow questions: 以及以下堆栈溢出问题:

I this this should do the trick 我这应该做的把戏

for (Map.Entry<String, ? extends A> entry : map.entrySet())
{
   String key = entry.getKey();
    A value = entry.getValue();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM