为什么不是Java集合remove方法的通用? [英] Why aren't Java Collections remove methods generic?

查看:143
本文介绍了为什么不是Java集合remove方法的通用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么没有<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/Collection.html#remove(java.lang.Object)\">Collection.remove(Object O)的通用?

好像收藏&LT; E&GT; 可以有布尔删除(E O);

然后,当你不小心尝试删除(例如)设置&LT;弦乐&GT; ,而不是从每个字符串一个收藏&LT;弦乐&GT; ,这将是一个编译时错误,而不是一个调试问题后。

Then, when you accidentally try to remove (for example) Set<String> instead of each individual String from a Collection<String>, it would be a compile time error instead of a debugging problem later.

推荐答案

乔希布洛赫和比尔·皮尤是指这个问题在 Java的谜题四:
幻影参考威胁,克隆人的进攻,并且复仇

Josh Bloch and Bill Pugh refer to this issue in Java Puzzlers IV: The Phantom Reference Menace, Attack of the Clone, and Revenge of The Shift.

乔希布洛赫说(6:41),他们试图泛型化get方法
地图,删除方法和其他一些,但它根本没有工作。

Josh Bloch says (6:41) that they attempted to generify the get method of Map, remove method and some other, but "it simply didn't work".

有可能无法如泛型太多的合理方案
你只允许泛型类型的集合作为参数类型。
他给出的例子是数量 S和一个列表的交集
列表 秒。

There are too many reasonable programs that could not be generified if you only allow the generic type of the collection as parameter type. The example given by him is an intersection of a List of Numbers and a List of Longs.

这篇关于为什么不是Java集合remove方法的通用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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