gpt4 book ai didi

java.util.Set 添加和删除方法签名区别

转载 作者:搜寻专家 更新时间:2023-11-01 03:50:11 25 4
gpt4 key购买 nike

当我在 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 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
}

我不明白,为什么 add 方法采用 E 参数而 remove 方法采用 Object 参数作为输入参数?

任何有助于理解此行为的帮助或引用链接将不胜感激。

最佳答案

因为您的 Set 将始终返回 E,但您可能想检查某个对象(不一定是 E)是否在 Set 中。

关于java.util.Set 添加和删除方法签名区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30781697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com