java.util.Set添加和删除方法签名差异
当我在JDK中看到Set.java文件时java.util.Set添加和删除方法签名差异,java,set,Java,Set,当我在JDK中看到Set.java文件时 /** * * <p>This interface is a member of the * <a href="{@docRoot}/../technotes/guides/collections/index.html"> * Java Collections Framework</a>. * * @param <E> the type of elements maintained by thi
/**
*
* <p>This interface is a member of the
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
* Java Collections Framework</a>.
*
* @param <E> the type of elements maintained by this set
*
* @author Josh Bloch
* @author Neal Gafter
* @see Collection
* @see List
* @see SortedSet
* @see HashSet
* @see TreeSet
* @see AbstractSet
* @see Collections#singleton(java.lang.Object)
* @see Collections#EMPTY_SET
* @since 1.2
*/
public interface Set<E> extends Collection<E> {
/**
* @param e element to be added to this set
* @return <tt>true</tt> if this set did not already contain the specified
* element
* @throws UnsupportedOperationException if the <tt>add</tt> operation
* is not supported by this set
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this set
* @throws NullPointerException if the specified element is null and this
* set does not permit null elements
* @throws IllegalArgumentException if some property of the specified element
* prevents it from being added to this set
*/
boolean add(E e);
/**
* @param o object to be removed from this set, if present
* @return <tt>true</tt> if this set contained the specified element
* @throws ClassCastException if the type of the specified element
* is incompatible with this set
* (<a href="Collection.html#optional-restrictions">optional</a>)
* @throws NullPointerException if the specified element is null and this
* set does not permit null elements
* (<a href="Collection.html#optional-restrictions">optional</a>)
* @throws UnsupportedOperationException if the <tt>remove</tt> operation
* is not supported by this set
*/
boolean remove(Object o);
//other methods
}
/**
*
*此接口是
* .
*
*@param此集合维护的元素类型
*
*@作者Josh Bloch
*@作者Neal Gafter
*@见收藏
*@见列表
*@见SortedSet
*@see HashSet
*@见树集
*@见抽象集
*@see Collections#singleton(java.lang.Object)
*@see集合#空集
*@自1.2
*/
公共接口集扩展集合{
/**
*@param e要添加到此集合的元素
*@如果此集合尚未包含指定的
*元素
*@如果添加操作失败,则会引发UnsupportedOperationException
*此集合不支持
*如果指定元素的类
*阻止将其添加到此集合
*如果指定的元素为null且此
*集合不允许空元素
*@如果指定元素的某些属性
*阻止将其添加到此集合
*/
布尔加法(E);
/**
*@param o要从此集合中删除的对象(如果存在)
*@如果此集合包含指定元素,则返回true
*@如果指定元素的类型为
*与此集不兼容
* ()
*如果指定的元素为null且此
*集合不允许空元素
* ()
*@如果删除操作失败,则会引发UnsupportedOperationException
*此集合不支持
*/
布尔移除(对象o);
//其他方法
}
我不明白,为什么add
方法将E
参数作为输入参数,而remove
方法将对象作为输入参数
任何帮助或参考链接,以了解这种行为将不胜感激 因为您的集合将始终返回E,但您可能需要检查集合中是否有对象(不一定是E)。我同意,这是wierd,但它允许您执行以下操作:
Set<String> stringSet = new HashSet<String>();
stringSet.add("blah");
Object blah = "blah";
stringSet.remove(blah);
Set stringSet=new HashSet();
stringSet.添加(“废话”);
对象blah=“blah”;
stringSet.移除(废话);
收集的add
方法是“协变”的,这意味着您可以添加E
类型或E
子类型的任何元素。
remove
方法是相反的,但是您可以删除属于E
类型或E
超类型的任何对象。通常,语法必须是
public boolean remove(? super E element)
但是,由于这种语法在Java中无效(不幸的是),因此参数的类型是Object
,无论E
是什么,它始终是E
的超级类型
这还允许您从运行时类型为E
的集合中删除对象,而其编译时类型是E
的超类型。例如,看看这段简单的代码
List<String> list = new ArrayList();
list.add("abc");
Object o = "abc";
list.remove(o);
这个例子编译得非常好,但毫无意义:您的列表中不可能有整数
结论
remove
的参数类型是Object
,因为它代表所有类型的超类型,并且允许您删除已升级为Object
的元素,而该元素由于其运行时类型仍然可能是集合中的元素。
签名remove(?super E)
既不可能也不必要,但它会在操作无法成功时警告程序员,如列表中的remove(1)
中的公共布尔加法(E)
E表示该集合维护的元素类型。只能在集合中使用类型为的允许元素。本质上,它的地图在起作用
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
在public boolean remove(Object o)
中,如果指定的元素存在,则从该集中移除该元素。你不需要检查它是什么类型的元素,只要检查它是否存在就可以了
再一次在内部工作
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
复制可能的复制,因为其内部映射需要“密钥”才能删除,检查我的答案。这没有任何区别…关键是键入“E”阅读这里的答案,所以你说集合这样做是因为地图这样做了-这是一个非答案,这肯定不是你的答案所说的:你从哪里得到了这个解释?我没有任何实际的来源,但这是最符合逻辑的解释。我会编辑我的答案来解释它。我理解你的想法,但我认为关于超类型这不是真的。它主要是关于安全性和向后兼容性。您确定它不能是公共布尔删除(t元素)
?@EpicPandaForce不幸的是,这也是无效的语法,但它与删除(?.super E)
表示相同的内容。
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}