java java泛型和addAll方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2701630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java generics and the addAll method
提问by neesh
What is the correct type of argument to the addAll(..) method in Java collections? If I do something like this:
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);
I understand I need to initialize both variables. However, I get a compilation error (from eclipse):
我知道我需要初始化两个变量。但是,我收到一个编译错误(来自 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[]>>)
what am I doing wrong?
我究竟做错了什么?
采纳答案by meriton
You can only insert instances of Tinto a List<T>.
您只能将 的实例插入T到List<T>.
The type List<? extends Map<X,Y>>stands for a list of an unknowntype Twhich extends Map<X,Y>. For instance, it could stand for a List<LinkedHashMap<X,Y>>. Obviously, you may not insert an ordinary HashMap<X,Y>into such a list.
类型List<? extends Map<X,Y>>代表未知类型的列表,T其中extends Map<X,Y>. 例如,它可以代表List<LinkedHashMap<X,Y>>. 显然,您不能HashMap<X,Y>在这样的列表中插入一个普通的。
You probably want:
你可能想要:
List<Map<String, Object[]>> currentList;
Or if you want to be really flexible you could do:
或者,如果你想变得非常灵活,你可以这样做:
List<? super Map<String, Object[]>> currentList;
Which would allow you to do crazy things like:
这会让你做一些疯狂的事情,比如:
currentList = new ArrayList<Map<? super String, ? extends Object[]>>();
You might also wish to read Angelika Langer's Generics FAQ, particularly the section about wildcards.
您可能还希望阅读 Angelika Langer 的泛型常见问题解答,尤其是有关通配符的部分。
回答by BalusC
Replace extendsby super.
替换extends为super。
The addAll()itself takes an ? extends Targument and that won't work when Tis ? extends Map<K, V>. With the change it will become effectively ? extends ? super Map<K, V>.
TheaddAll()本身需要一个? extends T参数,当Tis时它不起作用? extends Map<K, V>。随着变化它会变得有效? extends ? super Map<K, V>。
But still, better is just to remove that ? extends(or ? super). It's superfluous in this particular case. Just use List<Map<String, Object[]>>. The addAll()will then effectively take ? extends Map<String, Object[]>>.
但是,更好的是删除那个? extends(或? super)。在这种特殊情况下,这是多余的。只需使用List<Map<String, Object[]>>. 然后addAll()将有效地采取? extends Map<String, Object[]>>。
回答by newacct
just remember:
只要记住:
for <? extends ...>, you cannot put things (except null) into it
for <? extends ...>,你不能把东西(除了空)放进去
for <? super ...>, you cannot get things (except Object) out of it
因为<? super ...>,你不能从中得到东西(对象除外)
回答by doublep
You cannot call any method with generic attributes on such a list. Basically, what ? extends Foosays is "something that extends Foo", but you don't know what that something is. When that type is returned, you can say that returned value is Fooor some subtype. But for any given value you cannot that it matches ? extends Foogeneric.
您不能在这样的列表上调用具有通用属性的任何方法。基本上,? extends Foo所说的是“扩展的东西Foo”,但您不知道那是什么东西。当返回该类型时,您可以说返回值是Foo或某个子类型。但是对于任何给定的值,您不能认为它与? extends Foo泛型匹配。
In short: you need to change currentListdeclaration orcast it to List(withoutgenerics) and live with a warning or (if you know what you are doing) suppress it.
简而言之:您需要更改currentList声明或将其强制转换为List(没有泛型)并忍受警告或(如果您知道自己在做什么)抑制它。
回答by Jherico
Your question basically boils down to this:
你的问题基本上归结为:
public static <T extends Collection<?>> void foo(T t) {
T x = null;
x.addAll(t);
}
This will fail with essentially the same error as you describe. Try something along these lines to fix it.
这将失败,并出现与您描述的基本相同的错误。尝试按照这些方式进行修复。
public static <T extends Collection<Y>, Y> void foo(T t) {
T x = null;
x.addAll(t);
}
Alternatively, in your example above you could change
或者,在上面的示例中,您可以更改
List<? extends Map<String, Object[]>> currentList;
to
到
List<Map<String, Object[]>> currentList;
Generics gets hairy pretty quickly if you try to start passing around unspecified types.
如果你试图开始传递未指定的类型,泛型很快就会变得毛茸茸的。

