# 從列表中刪除所有出現的特定值

## 2.使用`while`循環

``````void removeAll(List<Integer> list, int element) {

while (list.contains(element)) {

list.remove(element);

}

}``````

``````// given

List<Integer> list = list(1, 2, 3);

int valueToRemove = 1;

// when

assertThatThrownBy(() -> removeAll(list, valueToRemove))

.isInstanceOf(IndexOutOfBoundsException.class);``````

``````void removeAll(List<Integer> list, Integer element) {

while (list.contains(element)) {

list.remove(element);

}

}``````

``````// given

List<Integer> list = list(1, 2, 3);

int valueToRemove = 1;

// when

removeAll(list, valueToRemove);

// then

assertThat(list).isEqualTo(list(2, 3));``````

``````void removeAll(List<Integer> list, Integer element) {

int index;

while ((index = list.indexOf(element)) >= 0) {

list.remove(index);

}

}``````

``````// given

List<Integer> list = list(1, 2, 3);

int valueToRemove = 1;

// when

removeAll(list, valueToRemove);

// then

assertThat(list).isEqualTo(list(2, 3));``````

## 3.刪除直到`List`更改

`List.remove(E element)`**具有我們尚未提及的功能：它返回一個`boolean`值，如果`List`由於操作而更改，返回`boolean`值，則為`true` ，因此其中包含element** 。

``````void removeAll(List<Integer> list, int element) {

while (list.remove(element));

}``````

``````// given

List<Integer> list = list(1, 1, 2, 3);

int valueToRemove = 1;

// when

removeAll(list, valueToRemove);

// then

assertThat(list).isEqualTo(list(2, 3));``````

## 3.使用`for`循環

``````void removeAll(List<Integer> list, int element) {

for (int i = 0; i < list.size(); i++) {

if (Objects.equals(element, list.get(i))) {

list.remove(i);

}

}

}``````

``````// given

List<Integer> list = list(1, 2, 3);

int valueToRemove = 1;

// when

removeAll(list, valueToRemove);

// then

assertThat(list).isEqualTo(list(2, 3));``````

``````// given

List<Integer> list = list(1, 1, 2, 3);

int valueToRemove = 1;

// when

removeAll(list, valueToRemove);

// then

assertThat(list).isEqualTo(list(1, 2, 3));``````

• `i = 0`
• `element``list.get(i)`在第3行都等於`1` ，因此Java輸入`if`語句的主體，
• 我們刪除索引為`0`的元素，
• 所以`list`現在包含`1``2``3`
• `i = 1`
• `list.get(i)`返回`2`因為當我們從`List`刪除一個元素時，它將所有進行中的元素移動到較小的索引

``````void removeAll(List<Integer> list, int element) {

for (int i = 0; i < list.size(); i++) {

if (Objects.equals(element, list.get(i))) {

list.remove(i);

i--;

}

}

}``````

``````void removeAll(List<Integer> list, int element) {

for (int i = 0; i < list.size();) {

if (Objects.equals(element, list.get(i))) {

list.remove(i);

} else {

i++;

}

}

}``````

``````// given

List<Integer> list = list(1, 1, 2, 3);

int valueToRemove = 1;

// when

removeAll(list, valueToRemove);

// then

assertThat(list).isEqualTo(list(2, 3));``````

• `ArrayList`刪除一個元素，將所有元素`ArrayList`它之後
• 通過`LinkedList`的索引訪問元素意味著逐一遍曆元素，直到找到索引

## 4.使用`for-each`循環

``````void removeAll(List<Integer> list, int element) {

for (Integer number : list) {

if (Objects.equals(number, element)) {

list.remove(number);

}

}

}``````

``````// given

List<Integer> list = list(1, 1, 2, 3);

int valueToRemove = 1;

// when

assertThatThrownBy(() -> removeWithForEachLoop(list, valueToRemove))

.isInstanceOf(ConcurrentModificationException.class);``````

`for-each`循環使用`Iterator`遍曆元素。但是，當我們修改`List``Iterator`進入不一致狀態。因此，它拋出`ConcurrentModificationException`

## 5.使用`Iterator`

``````void removeAll(List<Integer> list, int element) {

for (Iterator<Integer> i = list.iterator(); i.hasNext();) {

Integer number = i.next();

if (Objects.equals(number, element)) {

i.remove();

}

}

}``````

``````// given

List<Integer> list = list(1, 1, 2, 3);

int valueToRemove = 1;

// when

removeAll(list, valueToRemove);

// then

assertThat(list).isEqualTo(list(2, 3));``````

## 6.收集

``````List<Integer> removeAll(List<Integer> list, int element) {

List<Integer> remainingElements = new ArrayList<>();

for (Integer number : list) {

if (!Objects.equals(number, element)) {

}

}

return remainingElements;

}``````

``````// given

List<Integer> list = list(1, 1, 2, 3);

int valueToRemove = 1;

// when

List<Integer> result = removeAll(list, valueToRemove);

// then

assertThat(result).isEqualTo(list(2, 3));``````

• 它不會修改原始`List`但會返回一個新`List`
• 該方法決定返回的`List`的實現是什麼，它可能與原始的實現不同

``````void removeAll(List<Integer> list, int element) {

List<Integer> remainingElements = new ArrayList<>();

for (Integer number : list) {

if (!Objects.equals(number, element)) {

}

}

list.clear();

}``````

``````// given

List<Integer> list = list(1, 1, 2, 3);

int valueToRemove = 1;

// when

removeAll(list, valueToRemove);

// then

assertThat(list).isEqualTo(list(2, 3));``````

## 7.使用Stream API

Java 8引入了lambda表達式和流API。有了這些強大的功能，我們可以使用非常乾淨的代碼解決問題：

``````List<Integer> removeAll(List<Integer> list, int element) {

return list.stream()

.filter(e -> !Objects.equals(e, element))

.collect(Collectors.toList());

}``````

``````// given

List<Integer> list = list(1, 1, 2, 3);

int valueToRemove = 1;

// when

List<Integer> result = removeAll(list, valueToRemove);

// then

assertThat(result).isEqualTo(list(2, 3));``````

## 8.使用`removeIf`

``````void removeAll(List<Integer> list, int element) {

list.removeIf(n -> Objects.equals(n, element));

}``````

``````// given

List<Integer> list = list(1, 1, 2, 3);

int valueToRemove = 1;

// when

removeAll(list, valueToRemove);

// then

assertThat(list).isEqualTo(list(2, 3));``````