java.util.Set 添加和删除方法签名差异 [英] java.util.Set add and remove method signature difference

查看:48
本文介绍了java.util.Set 添加和删除方法签名差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我看到 JDK 中的 Set.java 文件时,

When I see the Set.java file in JDK,

/**
 *
 * <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 参数作为输入参数?

I am not getting, why add method is taking E parameter and remove method is taking Object parameter as a input argument?

任何帮助或参考链接来理解这种行为将不胜感激.

Any help or reference link to understand this behavior would be appreciate.

推荐答案

集合的 add 方法是 'covariant',这意味着你可以添加 EE 的子类型.然而,remove 方法是逆变的,您可以删除属于 E 类型或 E 超类型的任何对象.通常,语法必须是

The add method of collections is 'covariant', which means that you can add any element of type E or of a subtype of E. The remove method however is contravariant, you can remove any object that is a either of type E or of a supertype of E. Normally, the syntax would have to be

public boolean remove(? super E element)

但是由于这个语法在Java中是无效的(不幸的是),参数的类型是Object,它总是E的超类型,无论如何E 是.

But since this syntax is not valid in Java (unfortunately), the type of the parameter is Object, which is always a super type of E, no matter what E is.

这还允许您从集合中删除运行时类型为 E 而其编译时类型是 E 的超类型的对象.比如看一下这段简单的代码

This also allows you to remove an object from the collection whose runtime type is E while its compile-time type is a supertype of E. For example, take a look at this simple piece of code

List<String> list = new ArrayList();
list.add("abc");

Object o = "abc";
list.remove(o);

此代码表明您应该能够从集合中删除具有相同或超类型集合的任何对象.由于 Java 的语法限制,该方法还会接受由于其类型而永远不会出现在集合中的任何其他对象:

This code shows that you should be able to remove any object from the collection that has the same or a supertype of the collection. Due to the syntactic limitation of Java, the method would also accept any other object that could never be in the collection due to its type:

list.remove(1);

这个例子编译得很好,但它没有意义:你的List中永远不可能有一个整数.

This example compiles perfectly fine, but it makes no sense: You could never have an Integer in your List<String>.

结论

remove 的参数类型是 Object 因为它代表了所有类型的超类型,它可以让你删除一个已经向上转换为 Object 由于其运行时类型,它仍然可能是集合中的一个元素.签名 remove(? super E) 既不可能也不必要,但它会在操作永远不会成功时警告程序员,例如 remove(1)>列表<字符串>.

The parameter type of remove is Object because it represents the supertype of all types, and it lets you remove an element that has been upcasted to Object while it could still be an element in the collection due to its runtime type. The signature remove(? super E) is neither possible nor necessary, but it would warn the programmer when an operation can never succeed, like remove(1) on a List<String>.

这篇关于java.util.Set 添加和删除方法签名差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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