泛型不能实例化新的HashMap< String,?>`。为什么? [英] Generics can't instantiate `new HashMap<String, ?>`. Why?

查看:139
本文介绍了泛型不能实例化新的HashMap< String,?>`。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面这行代码在HashMap中给出错误?

 地图< String,?> map = new HashMap< String,?>(); 


解决方案

你不能像那样实例化一个参数化类型。通配符参数化类型不是具体类型,因此它们不能出现在新的表达式中。

通配符参数化类型表示一类包含泛型类型的具体实例的类型。正在使用的通配符的种类决定了哪些具体的参数化类型属于该族。

因此, Map< String,?> 表示类型族,可以是 Map< String,Object> Map< String,String> ,任何东西。您必须在创建实际地图时使用具体类型。



所以,您可以这样做:

 地图<字符串,> map = new HashMap< String,String>(); 
地图< String,?> map = new HashMap< String,Object>();

但是这个声明的问题在于,除了<$ c



另一方面,考虑其他类型的通配符 - upper bounded lower bound

上界通配符



 映射< String,?扩展Number> 

这种类型表示的系列是具体的实例化,其中值类型是 Number ,或者是 Number 的子类型。因此,对于这一个,下面是具体参数化类型的有效创建:

  Map< String,?扩展Number> map = new HashMap< String,Number>(); 
地图< String,?扩展Number> map = new HashMap< String,Integer>();

但是,再次问题是您不能在地图中添加任何东西,除 null 或之前从中提取的值。






下界限通配符



  Map< String,?超整型> 

正如您已经知道的那样,您无法使用无界通配符将任何内容添加到通配符参数化类型,或者上限通配符。还有第三种类型 - 下界有界的通配符,如果要在地图上添加某些内容,则可以使用该通配符。上述参数化类型的类型族是获取类型 Integer 或超类型值的类型。所以,你可以这样实例化:

  Map< String,?超整型> map = new HashMap< String,Number>(); 
地图< String,?超整型> map = new HashMap< String,Integer>();
地图< String,?超整型> map = new HashMap< String,Serializable>();

但这里还有一个问题。尽管你可以给它增加一个值,但是当你获取一些值时,你不能确定你得到的是什么类型。






<因此,根据您的要求,您可能必须使用其中一种或另一种类型的通配符。可能是一些更多的信息在这个问题可能会帮助你。但是,一般只记得,你不能创建通配符参数化类型的对象,而只能创建具体的参数化类型。


Why does the following line of code give an error in HashMap? It is not taking the wild card in the right hand side of the assignment.

Map<String,?> map=new HashMap<String,?>();

解决方案

You can't instantiate a parameterized type like that. A wildcard parameterized type is not a concrete type, so they could not appear in a new expression.

A wildcard parameterized type denotes a family of types comprising concrete instantiations of a generic type. The kind of the wildcard being used determines which concrete parameterized types belong to the family.

So, Map<String,?> denotes the family of types, which can be Map<String, Object>, Map<String, String>, anything. You have to use the concrete type while creating an actual map.

So, you can do:

Map<String,?> map=new HashMap<String, String>();
Map<String,?> map=new HashMap<String, Object>();

But the issue with this declaration is that, you can't add anything to map except null.


On the other hand, consider other types of wildcard - upper bounded, and lower bounded.

Upper Bounded Wildcard:

Map<String, ? extends Number>

The family of types denoted by this type are the concrete instantiation, where the value type is either Number, or a subtype of Number. So, for this one, the following are valid creation of concrete parameterized type:

Map<String, ? extends Number> map = new HashMap<String, Number>();
Map<String, ? extends Number> map = new HashMap<String, Integer>();

But again, issue here is you can't add anything in the map except null or the value previously fetched from it.


Lower Bounded wildcard:

Map<String, ? super Integer>

As you already know, you can't add anything to the wildcard parameterized type with unbounded wildcard, or upper bounded wildcard. There is a third type - lower bounded wildcard, which can be used if you are going to add something to the map. The family of type for the above parameterized type are the one that takes value of type Integer or it's super type. So, you can instantiate it like this:

Map<String, ? super Integer> map = new HashMap<String, Number>();
Map<String, ? super Integer> map = new HashMap<String, Integer>();
Map<String, ? super Integer> map = new HashMap<String, Serializable>();

But there is another issue here. Although you can add a value to it, but when you fetch some value, you cannot be sure what type you get.


So, depending upon your requirement, you may have to use one or the other type of wildcard. May be some more information in the question might help you. But, in general just remember, you can't create an object of wildcard parameterized type, but only concrete parameterized type.

这篇关于泛型不能实例化新的HashMap&lt; String,?&gt;`。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆