英文:
Why a MAP use generic wildcard extends Object cannot hold any type?
问题
抱歉,如果问题标题让您感到困惑。这是我的意思。
我有一个 Map<String, ? extends Object> studentDetails = new HashMap<>();
,我希望它可以保存任意随机类型。
当我添加时
studentDetails.put("name", "John");
studentDetails.put("friendNames", Arrays.aslist("Kenny", "Peter", "friend3"));
studentDetails.put("courses", new HashSet<String>());
studentDetails.put("age", 18);
编译器报错 :
> 需要的类型:? extends Object 的捕获,提供的类型:
> String
> 需要的类型:? extends Object 的捕获,提供的类型:
> ArrayList
> 需要的类型:? extends Object 的捕获,提供的类型:
> HashSet
> 需要的类型:? extends Object 的捕获,提供的类型:
> Integer
为什么会出错呢?? extends Object
不应该可以捕获任何类型吗?如果我将其更改为 Map<String, Object>
,那么就可以工作。
任何帮助将不胜感激!谢谢!
英文:
Sorry if the question title is confusing. Here is what I mean.
I have a Map<String, ? extends Object> studentDetails = new HashMap<>();
and I want it to hold any random types.
When I put
studentDetails.put("name", "John");
studentDetails.put("friendNames", Arrays.aslist("Kenny","Peter","friend3"));
studentDetails.put("courses", new HashSet<String>());
studentDetails.put("age",18);
Compiler gives me :
> required type: capture of ? extends Object, Provided:
> String
> required type: capture of ? extends Object, Provided:
> ArrayList
> required type: capture of ? extends Object, Provided:
> HashSet
> required type: capture of ? extends Object, Provided:
> Integer
Why is it wrong? Shouldn't ? extends Object
captures any type? If I change it to Map<String, Object>, then it works.
Any help would be appreciated! Thanks!
答案1
得分: 2
由于相同的原因,您无法执行以下操作:
List<Integer> ints = new ArrayList<>();
List<? extends Object> objs;
List<Integer>
是 List<? extends Object>
的子类型,因此您可以将 List<Integer>
的实例分配给类型为 List<? extends Object>
的变量。但是,您不能执行以下操作,因为这会将一个字符串添加到整数列表中。编译器会捕获到这个错误。
objs = ints; // 可以
objs.add("abc"); // 不能将字符串添加到整数列表中
对于映射也是如此。
Map<String, ? extends Object> map;
Map<String, Integer> intMap = new HashMap<>();
map = intMap; // 可以
map.put("abc", "abc"); // 错误
编译器不知道您的映射引用的是什么类型,因此无法向其添加对象。
英文:
For the same reason you can't do this:
List<Integer> ints = new ArrayList<>();
List<? extends Object> objs;
List<Integer>
is a subtype of List<? extends Object>
so you can assign an instance of List<Integer>
to a type List<? extends Object>
. But you can't do the following because you would be adding a String into a list of Integers. And the compiler catches it.
objs = ints; // okay
objs.add("abc"); // can't add String to list of ints
The same is true for maps.
Map<String, ? extends Object> map;
Map<String, Integer> intMap = new HashMap<>();
map = intMap; // okay
map.put("abc", "abc"); //oops
The compiler has no idea what your map refers to so you can't add an object to it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论