1. Overview
1.概述
In this tutorial, we’re going to see how to remove elements from an ArrayList in Java using different techniques. Given a list of sports, let’s see how we can get rid of some elements of the following list:
在本教程中,我们将看到如何使用不同的技术在Java中从ArrayList中删除元素。给定一个运动列表,让我们看看如何摆脱以下列表中的一些元素。
List<String> sports = new ArrayList<>();
sports.add("Football");
sports.add("Basketball");
sports.add("Baseball");
sports.add("Boxing");
sports.add("Cycling");
2. ArrayList#remove
2.ArrayList#remove
ArrayList has two available methods to remove an element, passing the index of the element to be removed, or passing the element itself to be removed, if present. We’re going to see both usages.
ArrayList有两个可用的方法来移除一个元素,传入要移除的元素的索引,或者传入要移除的元素本身(如果存在)。我们将看到这两种用法。
2.1. Remove by Index
2.1.按索引删除
Using remove passing an index as parameter, we can remove the element at the specified position in the list and shift any subsequent elements to the left, subtracting one from their indices. After execution, remove method will return the element that has been removed:
使用remove传递一个索引作为参数,我们可以移除列表中指定位置的元素,并将任何后续元素向左移动,从它们的索引中减去1。执行后,remove方法将返回被移除的元素。
sports.remove(1); // since index starts at 0, this will remove "Basketball"
assertEquals(4, sports.size());
assertNotEquals(sports.get(1), "Basketball");
2.2. Remove by Element
2.2.按元素删除
Another way is to remove the first occurrence of an element from a list using this method. Formally speaking, we’re removing the element with the lowest index if exists, if not, the list is unchanged:
另一种方法是使用这个方法从列表中移除第一次出现的元素。从形式上讲,如果存在的话,我们将删除索引最低的元素,如果不存在的话,列表将保持不变。
sports.remove("Baseball");
assertEquals(4, sports.size());
assertFalse(sports.contains("Baseball"));
3. Removing While Iterating
3.迭代过程中的删除
Sometimes we want to remove an element from an ArrayList while we’re looping it. Due to not generate a ConcurrentModificationException, we need to use Iterator class to do it properly.
有时我们想在循环过程中从ArrayList中移除一个元素。由于不产生ConcurrentModificationException,我们需要使用Iterator类来正确完成。
Let’s see how we can get rid of an element in a loop:
让我们看看如何摆脱循环中的一个元素。
Iterator<String> iterator = sports.iterator();
while (iterator.hasNext()) {
if (iterator.next().equals("Boxing")) {
iterator.remove();
}
}
4. ArrayList#removeIf (JDK 8+)
4.ArrayList#removeIf(JDK 8以上)
If we’re using JDK 8 or higher versions, we can take advantage of ArrayList#removeIf which removes all of the elements of the ArrayList that satisfy a given predicate.
如果我们使用JDK 8或更高版本,我们可以利用ArrayList#removeIf,它可以移除ArrayList中满足指定谓词的所有元素。
sports.removeIf(p -> p.equals("Cycling"));
assertEquals(4, sports.size());
assertFalse(sports.contains("Cycling"));
Finally, we can do it using third party libraries like Apache Commons and, if we want to go deeper, we can see how to remove all specific occurrences in an efficient way.
最后,我们可以通过使用第三方库(如Apache Commons)来实现,如果我们想深入了解,我们可以看到如何以高效的方式删除所有特定的出现。
5. Conclusion
5.总结
In this tutorial, we looked at the various ways of removing elements from an ArrayList in Java.
在本教程中,我们看了在Java中从ArrayList中删除元素的各种方法。
As usual, all the examples used at this tutorial are available on GitHub.
像往常一样,本教程中使用的所有例子都可以在GitHub上找到。