![](/img/trans.png)
[英]Difference between defining configuration as Properties and Map object
[英]difference between Map<?,?> and Map<Object,Object>
当我声明一个Map或Map <Object,Object>时,我可以在这个Map中放置任何东西
Map map = new HashMap();
map.put("");
但是如果我把它声明为Map <?,?>我就什么都不用了
Map<?,?> map = new HashMap();
map.put("");
它会出错吗?
Map<?,?> map = new HashMap<Integer, Integer>(); // compiles just fine!
?
代表一些固定但未知的类型。 你不能把""
放在Map<Integer, Integer>
, Map<?, ?>
可以是任何类型的Map
,包括Map<Integer, Integer>
。
在java集合框架中,? 意思是未知类型。 您只能从中读取元素,但不能添加除NULL值以外的元素。
所以你可以编译如下:
Map<?,?> map = new HashMap();
map.put(null, null);
对于此代码段:
Map<?,?> map = new HashMap();
Map<?,?>
表示键入未知类型的Map。
问号(?),称为通配符,通配符表示“值类型参数可以是任何东西”,它并不意味着“你可以使用它,好像它是你想要它的任何东西”。
有关更多信息,请转到链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.