java 使用带通配符的通用地图的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1921237/
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
Problem using generic map with wildcard
提问by mmoossen
I have a method that returns a mapdefined as:
我有一个方法返回一个map定义为:
public Map<String, ?> getData();
The actual implementation of this method is not clear to me, but, when I try to do:
我不清楚此方法的实际实现,但是,当我尝试执行以下操作时:
obj.getData().put("key","value")
I get following compile time error message:
我收到以下编译时错误消息:
The method put(String, capture#9-of ?) in the type Map is not applicable for the arguments (String, String)
类型 Map 中的 put(String, capture#9-of ?) 方法不适用于参数 (String, String)
What is the problem? Is Stringnot of type anything?
问题是什么?是String不是什么类型的东西?
Thanks in advance.
提前致谢。
采纳答案by Jon Skeet
The wildcard means "the value type parameter could be anything" - it doesn'tmean "you can use this as if it were anything you want it to be". In other words, a Map<String, UUID>is valid as a Map<String, ?>- but you wouldn't want to be able to put a String value into it.
通配符的意思是“值类型参数可以是任何东西”——它并不意味着“你可以把它当作你想要的任何东西来使用”。换句话说, aMap<String, UUID>作为 a Map<String, ?>-有效,但您不希望能够将 String 值放入其中。
If you want a map which can definitely accept string values, you want:
如果您想要一个绝对可以接受字符串值的地图,您需要:
Map<String, ? super String>
回答by Ben Lings
The return type of
返回类型为
Map<String, ?>
is the same as
是相同的
Map<String, ? extends Object>
The means that the concrete type returned could be a Map<String, AnyClass>. You can't put a Stringinto an AnyClass, hence the error.
这意味着返回的具体类型可能是Map<String, AnyClass>. 您不能将 aString放入 an AnyClass,因此会出现错误。
A good general principle is to not use wildcards in method return types.
一个好的一般原则是不要在方法返回类型中使用通配符。
回答by Hardcoded
Map<String, ?>is a short form of Map<String,? extends Object>and doesn't mean that anything can be added as value. It says that the Map-object can have any generic value type extending Object.
Map<String, ?>是一种缩写形式,Map<String,? extends Object>并不意味着任何东西都可以作为价值添加。它说 Map 对象可以有任何扩展的泛型值类型Object。
This means that the Map object can be a HashMap<String, String>or a HashMap<String, Integer>as well. Because the compiler can't check which value types will be accepted, he won't let you call methods with the value type as a parameter.
这意味着 Map 对象也可以是 aHashMap<String, String>或 a HashMap<String, Integer>。因为编译器无法检查将接受哪些值类型,所以他不会让您以该值类型作为参数调用方法。
Note:
笔记:
- You can call methods with the value type as a return value, because everything must extend Object (? extends Object)
- A
Map<String, ? super String>will have the opposite effect: You can always use a String as parameter, but the return-type is unclear.
- 可以用值类型作为返回值调用方法,因为一切都必须扩展Object(? extends Object)
- A
Map<String, ? super String>会产生相反的效果:您始终可以使用 String 作为参数,但返回类型不清楚。
回答by Juha Syrj?l?
Try this:
试试这个:
public Map<String, Object> getData();
回答by enguerran
[EDIT]This is really wrong... I understood.
[编辑]这真的是错误的......我明白了。
My first answer was:
我的第一个回答是:
That's java : String is not an object.
Try with
obj.getData().put("key",new String("value"));
那是 java : String 不是对象。
试试
obj.getData().put("key",new String("value"));
But String extends Object... while I thought String was a primitive. I learned something ^^
但是 String 扩展了 Object... 而我认为 String 是一个原始类型。我学到了东西^^

