加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 这些泛型语句之间有什么区别?

发布时间:2020-12-15 04:42:37 所属栏目:Java 来源:网络整理
导读:参见英文答案 Is this raw type assignment type-safe? ListT = new ArrayList();????????????????????????????????????1个 MapString,Integer map = new HashMapString,Integer(); 和 MapString,Integer map = new HashMap(); 由于这对我来说是新的,我不确
参见英文答案 > Is this raw type assignment type-safe? List<T> = new ArrayList();????????????????????????????????????1个

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

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

由于这对我来说是新的,我不确定这两个陈述之间的实际区别是什么,因为两者似乎都运行良好.我试图在其他地方找到,但找不到任何具体的答案.

解决方法

第二个变体使用 raw type,这通常很糟糕.实际上,原始类型仅出于向后兼容性的原因而存在.

考虑一下:

Map<Double,Double> other = new HashMap<Double,Double>();
other.put(42.0,42.0);

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

使用原始类型,我们设法将双打放入字符串和整数之间的映射!

我们无法使用正确的参数化类型执行此操作:

Map<Double,Integer>(other);  // error

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读