java 泛型和 addAll 方法

发布于 2024-08-30 05:35:53 字数 1040 浏览 4 评论 0 原文

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[]>>)

我做错了什么?

What is the correct type of argument to the addAll(..) method in Java collections? If I do something like this:

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):

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

狼性发作 2024-09-06 05:35:53

extends 替换为 super

addAll() 本身需要一个 ?扩展 T 参数,当 T 时,这不起作用?扩展 Map。经过更改后,它将变为有效的 ?延伸?超级地图

但是,最好还是删除那个 ?扩展(或?super)。在这种特殊情况下这是多余的。只需使用 List> 即可。 addAll() 将有效地获取 ?扩展 Map>

Replace extends by super.

The addAll() itself takes an ? extends T argument and that won't work when T 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 use List<Map<String, Object[]>>. The addAll() will then effectively take ? extends Map<String, Object[]>>.

不乱于心 2024-09-06 05:35:53

只需记住:

对于 ,你不能在

< 中放入东西(除了 null )? super ...>,你无法从中获取东西(除了 Object)

just remember:

for <? extends ...>, you cannot put things (except null) into it

for <? super ...>, you cannot get things (except Object) out of it

扶醉桌前 2024-09-06 05:35:53

您只能将 T 的实例插入到 List 中。

类型 List> 代表未知类型T的列表,它扩展了Map。例如,它可以代表 List>。显然,您不能将普通的 HashMap 插入到这样的列表中。

您可能想要:

List<Map<String, Object[]>> currentList;

或者如果您想要真正灵活,您可以这样做:

List<? super Map<String, Object[]>> currentList;

这将允许您做一些疯狂的事情,例如:

currentList = new ArrayList<Map<? super String, ? extends Object[]>>();

您可能还希望阅读 Angelika Langer 的泛型常见问题解答,特别是 有关通配符的部分

You can only insert instances of T into a List<T>.

The type List<? extends Map<X,Y>> stands for a list of an unknown type T which 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.

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.

骑趴 2024-09-06 05:35:53

您不能调用此类列表中具有通用属性的任何方法。基本上,什么? 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 extends Foo", but you don't know what that something is. When that type is returned, you can say that returned value is Foo 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 to List (without generics) and live with a warning or (if you know what you are doing) suppress it.

所有深爱都是秘密 2024-09-06 05:35:53

您的问题基本上可以归结为:

public static <T extends Collection<?>> void foo(T t) {
T x = null;
x.addAll(t);
}

这将失败,并出现与您所描述的基本相同的错误。尝试按照这些思路进行修复。

public static <T extends Collection<Y>, Y> void foo(T t) {
T x = null;
x.addAll(t);
}

或者,在上面的示例中,

List<? extends Map<String, Object[]>> currentList;

您可以更改为泛型很快就会变得很麻烦。

List<Map<String, Object[]>> currentList;

如果您尝试开始传递未指定的类型,

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文