为什么 List.add(E) 返回 boolean 而 List.Add(int, E) 返回 void? [英] Why does List.add(E) return boolean while List.Add(int, E) returns void?

查看:44
本文介绍了为什么 List.add(E) 返回 boolean 而 List.Add(int, E) 返回 void?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 javadoc 我看到一个 ArrayList 有一个重载的 add 方法:

Looking at the javadoc I saw the that an ArrayList has an overloaded add method:

public boolean add(E e)

public boolean add(E e)

将指定的元素附加到此列表的末尾.

Appends the specified element to the end of this list.

public void add(int index,E元素)

public void add(int index, E element)

在此列表中的指定位置插入指定元素.将当前在该位置的元素(如果有)和任何后续元素向右移动(在它们的索引上加一).

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

我注意到第一个返回一个 boolean 而第二个返回一个 void.事实证明,第一个 add 必须返回一个 boolean 因为:

I noticed that the first one returned a boolean while the second one was a void. As it turns out, the first add HAS to return a boolean because:

返回:真(由 Collection.add(E) 指定)

Returns: true (as specified by Collection.add(E))

所以我去了 Collection.add(E):

布尔加法(E e)

确保此集合包含指定的元素(可选操作).如果此集合因调用而更改,则返回 true.(如果此集合不允许重复且已包含指定元素,则返回 false.)

Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

所以我的问题是,为什么指定 add 返回布尔值而不是空值?当我 add 一些东西时,我希望只做一个操作.

So my question is, why is add specified to return boolean instead of being a void? When I add something I would expect to only do an operation.

我知道还有其他数据结构,与 ArrayList 不同,不允许重复(例如集合).但即便如此,问题也不能按照以下方式解决:

I understand that there's other data structures that, as opposed to ArrayList, do not allow duplicates (such as sets). But even then, couldn't the problem be solved along the lines of:

public void add(E e){
    if(e is not in set){
        add e;
    }
}

这样,如果 e 在集合中,则不采取任何行动.为什么返回 boolean 而不是 void 方法更好?

That way if e IS in the set no action is taken. Why is it better to return a boolean instead of the void approach?

推荐答案

Collection.add 是一个非常通用的方法(不是在 Java 泛型的意义上——在广泛适用的意义上).因此,他们想要一个普遍适用的返回值.

Collection.add is a pretty generic method (not in the sense of Java generics -- in the sense of being widely applicable). As such, they wanted a return value that would apply generally.

某些类(如ArrayList)总是接受元素,因此总是返回true.你是对的,在这些情况下,返回类型 void 也一样好.

Some classes (like ArrayList) always accept elements, and so will always return true. You're right that in these cases, a return type of void would be just as good.

但是其他的,比如Set,有时不允许添加元素.在 Set 的情况下,如果一个相等的元素已经存在,就会发生这种情况.知道这一点通常很有帮助.另一个例子是有界集合(只能容纳一定数量的元素).

But others, like Set, will sometimes not allow an element to be added. In Set's case, this happens if an equal element was already present. It's often helpful to know that. Another example is a bounded collection (that can only hold a certain number of elements).

您可能会问,不能编码只是手动检查吗?"例如,有一个集合:

You could ask, "can't code just check this manually?" For instance, with a set:

if (!set.contains(item)) {
    set.add(item);
    itemWasAdded(item);
}

这比你现在可以做的更冗长,但不是很多:

This is more verbose than what you can do now, but not a whole lot:

if (set.add(item)) {
    itemWasAdded(item);
}

但是这种先检查后行动的行为不是线程安全的,这在多线程应用程序中可能至关重要.例如,可能是另一个线程在您检查第一个代码片段中的 set.contains(item)set.add(item) 之间添加了一个相等的项目.在多线程场景中,这两个操作确实需要是单个原子操作;从方法返回 boolean 使这成为可能.

But this check-then-act behavior isn't thread safe, which can be crucial in multithreaded applications. For instance, it could be that another thread added an equal item between you checking set.contains(item) and the set.add(item) in the first code snippet. In a multithreaded scenario, those two actions really need to be a single, atomic action; returning boolean from the method makes that possible.

这篇关于为什么 List.add(E) 返回 boolean 而 List.Add(int, E) 返回 void?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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