java map

来源:百度知道 编辑:UC知道 时间:2024/05/15 20:01:02
小弟在程序中用到了Map map=new HashMap();但是在Map和HashMap下有警告

《Map is a raw type. References to generic type Map<K,V> should be parameterized

HashMap is a raw type. References to generic type HashMap<K,V> should be parameterized》

如何修改啊?哪个大哥帮小弟一下。
程序下边还有一处用到MAP:map.put(item.getId(), item)

警告是《Type safety: The method put(Object, Object) belongs to the raw type Map. References to generic type Map<K,V> should be parameterized》

这个MAP的功能是提取图书类别名称,(例如:1对应计算机类,2对就文学类)

Map<K,V>其中K,V各是什么类型

Map<K,V>
类型参数:
K - 此映射所维护的键的类型
V - 映射值的类型

HashMap必须有key和value的

这个是java 泛型的问题,从jdk1.5 以后引入的.
比如:
Map<String,String> map = new HashMap<String,String>();
这个就是泛型,<>中是数据类型,可以是全部合法的java类型,
从而限定这个map只能放入什么类型的数据,这个就是只能是String类型的数据.
别的数据会报错,提高安全性!!

没用泛型!
数据不安全!

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