简体   繁体   English

为什么List.Add(E)返回布尔值而List.Add(int,E)返回void?

[英]Why does List.add(E) return boolean while List.Add(int, E) returns void?

Looking at the javadoc I saw the that an ArrayList has an overloaded add method: 看看javadoc我看到一个ArrayList有一个重载的add方法:

public boolean add(E e) public boolean add(E e)

Appends the specified element to the end of this list. 将指定的元素追加到此列表的末尾。

and

public void add(int index, E element) 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). 将当前位置的元素(如果有)和任何后续元素向右移动(将其添加到其索引中)。

I noticed that the first one returned a boolean while the second one was a void . 我注意到第一个返回了一个boolean而第二个返回了一个void As it turns out, the first add HAS to return a boolean because: 事实证明,第一个add HAS返回一个boolean因为:

Returns: true (as specified by Collection.add(E)) 返回:true(由Collection.add(E)指定)

So I went to Collection.add(E) : 所以我去了Collection.add(E)

boolean add(E e) 布尔加法(E e)

Ensures that this collection contains the specified element (optional operation). 确保此集合包含指定的元素(可选操作)。 Returns true if this collection changed as a result of the call. 如果此集合因调用而更改,则返回true。 (Returns false if this collection does not permit duplicates and already contains the specified element.) (如果此集合不允许重复并且已包含指定的元素,则返回false。)

So my question is, why is add specified to return boolean instead of being a void? 所以我的问题是,为什么add指定返回boolean而不是void? When I add something I would expect to only do an operation. 当我add一些东西时,我希望只做一个操作。

I understand that there's other data structures that, as opposed to ArrayList, do not allow duplicates (such as sets). 据我所知,还有其他数据结构,与ArrayList相反,不允许重复(例如集合)。 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;
    }
}

That way if e IS in the set no action is taken. 这样,如果集合中的e IS没有采取任何行动。 Why is it better to return a boolean instead of the void approach? 为什么返回boolean而不是void方法更好?

Collection.add is a pretty generic method (not in the sense of Java generics -- in the sense of being widely applicable). Collection.add是一种非常通用的方法(在Java泛型的意义上 - 在广泛适用的意义上)。 As such, they wanted a return value that would apply generally. 因此,他们想要一般适用的返回值。

Some classes (like ArrayList ) always accept elements, and so will always return true . 某些类(如ArrayList )始终接受元素,因此将始终返回true You're right that in these cases, a return type of void would be just as good. 你是对的,在这些情况下,返回类型的void也同样好。

But others, like Set , will sometimes not allow an element to be added. 但其他人,比如Set ,有时候不允许添加元素。 In Set 's case, this happens if an equal element was already present. Set的情况下,如果已经存在相等的元素,则会发生这种情况。 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);
}

But this check-then-act behavior isn't thread safe, which can be crucial in multithreaded applications. 但是这种check-then-act行为不是线程安全的,这在多线程应用程序中至关重要。 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. 例如,可能是另一个线程在您检查第一个代码片段中的set.contains(item)set.add(item)之间添加了相同的项。 In a multithreaded scenario, those two actions really need to be a single, atomic action; 在多线程场景中,这两个动作确实需要是单个原子动作; returning boolean from the method makes that possible. 从方法返回boolean使这成为可能。

因为知道是否实际添加了某些内容通常很有用,或者已经存在。

Because it's an extra information which doesn't cost anything and can be useful in certain situations. 因为这是一个额外的信息,不需要任何费用,在某些情况下可能会有用。 For example: 例如:

Set<String> set = new HashSet<String>();
set.add("foobar");
boolean wasAlreadyThere = set.add("foobar");

Otherwise you would have to do 否则你必须这样做

boolean wasAlreadyThere = set.contains("foobar");
set.add("foobar");

which requires twice the work (first you have to lookup, then lookup again to add). 这需要两倍的工作(首先你必须查找,然后再次查找添加)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM