java 泛型和 addAll 方法
Java 集合中 addAll(..) 方法的正确参数类型是什么?如果我这样做:
List<? extends Map<String, Object[]>> currentList = new ArrayList<Map<String, Object[]>>();
Collection<HashMap<String, Object[]>> addAll = new ArrayList<HashMap<String, Object[]>>();
// add some hashmaps to the list..
currentList.addAll(addAll);
我知道我需要初始化这两个变量。但是,我收到编译错误(来自 eclipse):
Multiple markers at this line
- The method addAll(Collection<? extends capture#1-of ? extends Map<String,Object[]>>) in the type List<capture#1-of ? extends Map<String,Object[]>> is not applicable for the arguments (List<capture#2-of ? extends
Map<String,Object[]>>)
- The method addAll(Collection<? extends capture#1-of ? extends Map<String,Object[]>>) in the type List<capture#1-of ? extends Map<String,Object[]>> is not applicable for the arguments
(Collection<HashMap<String,Object[]>>)
我做错了什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将
extends
替换为super
。addAll()
本身需要一个?扩展 T
参数,当T
为时,这不起作用?扩展 Map
。经过更改后,它将变为有效的?延伸?超级地图
。但是,最好还是删除那个
?扩展
(或?super
)。在这种特殊情况下这是多余的。只需使用List
即可。addAll()
将有效地获取?扩展 Map>
。Replace
extends
bysuper
.The
addAll()
itself takes an? extends T
argument and that won't work whenT
is? extends Map<K, V>
. With the change it will become effectively? extends ? super Map<K, V>
.But still, better is just to remove that
? extends
(or? super
). It's superfluous in this particular case. Just useList<Map<String, Object[]>>
. TheaddAll()
will then effectively take? extends Map<String, Object[]>>
.只需记住:
对于
,你不能在
< 中放入东西(除了 null )? super ...>
,你无法从中获取东西(除了 Object)just remember:
for
<? extends ...>
, you cannot put things (except null) into itfor
<? super ...>
, you cannot get things (except Object) out of it您只能将
T
的实例插入到List
中。类型
List>
代表未知类型
T
的列表,它扩展了Map
。例如,它可以代表List>
。显然,您不能将普通的HashMap
插入到这样的列表中。您可能想要:
或者如果您想要真正灵活,您可以这样做:
这将允许您做一些疯狂的事情,例如:
您可能还希望阅读 Angelika Langer 的泛型常见问题解答,特别是 有关通配符的部分。
You can only insert instances of
T
into aList<T>
.The type
List<? extends Map<X,Y>>
stands for a list of an unknown typeT
whichextends Map<X,Y>
. For instance, it could stand for aList<LinkedHashMap<X,Y>>
. Obviously, you may not insert an ordinaryHashMap<X,Y>
into such a list.You probably want:
Or if you want to be really flexible you could do:
Which would allow you to do crazy things like:
You might also wish to read Angelika Langer's Generics FAQ, particularly the section about wildcards.
您不能调用此类列表中具有通用属性的任何方法。基本上,什么
? extends Foo
说的是“扩展Foo
的东西”,但你不知道那是什么。当返回该类型时,您可以说返回值是 Foo 或某个子类型。但对于任何给定的值,你不能让它匹配?扩展 Foo
通用。简而言之:您需要更改
currentList
声明或将其转换为List
(不带泛型)并使用警告或(如果你知道自己在做什么)抑制它。You cannot call any method with generic attributes on such a list. Basically, what
? extends Foo
says is "something that extendsFoo
", but you don't know what that something is. When that type is returned, you can say that returned value isFoo
or some subtype. But for any given value you cannot that it matches? extends Foo
generic.In short: you need to change
currentList
declaration or cast it toList
(without generics) and live with a warning or (if you know what you are doing) suppress it.您的问题基本上可以归结为:
这将失败,并出现与您所描述的基本相同的错误。尝试按照这些思路进行修复。
或者,在上面的示例中,
您可以更改为泛型很快就会变得很麻烦。
如果您尝试开始传递未指定的类型,
Your question basically boils down to this:
This will fail with essentially the same error as you describe. Try something along these lines to fix it.
Alternatively, in your example above you could change
to
Generics gets hairy pretty quickly if you try to start passing around unspecified types.