* bael-2023: removing all occurrences of a value from a list

* adjusting examples to match the article
This commit is contained in:
fanatixan 2018-07-31 12:56:29 +02:00 committed by Grzegorz Piwowarek
parent fb7b7c3ec5
commit 6e646bf0de
2 changed files with 319 additions and 0 deletions

View File

@ -0,0 +1,111 @@
package com.baeldung.list;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class RemoveAll {
static void removeWithWhileLoopPrimitiveElement(List<Integer> list, int element) {
while (list.contains(element)) {
list.remove(element);
}
}
static void removeWithWhileLoopNonPrimitiveElement(List<Integer> list, Integer element) {
while (list.contains(element)) {
list.remove(element);
}
}
static void removeWithWhileLoopStoringFirstOccurrenceIndex(List<Integer> list, Integer element) {
int index;
while ((index = list.indexOf(element)) >= 0) {
list.remove(index);
}
}
static void removeWithCallingRemoveUntilModifies(List<Integer> list, Integer element) {
while (list.remove(element))
;
}
static void removeWithStandardForLoopUsingIndex(List<Integer> list, int element) {
for (int i = 0; i < list.size(); i++) {
if (Objects.equals(element, list.get(i))) {
list.remove(i);
}
}
}
static void removeWithForLoopDecrementOnRemove(List<Integer> list, int element) {
for (int i = 0; i < list.size(); i++) {
if (Objects.equals(element, list.get(i))) {
list.remove(i);
i--;
}
}
}
static void removeWithForLoopIncrementIfRemains(List<Integer> list, int element) {
for (int i = 0; i < list.size();) {
if (Objects.equals(element, list.get(i))) {
list.remove(i);
} else {
i++;
}
}
}
static void removeWithForEachLoop(List<Integer> list, int element) {
for (Integer number : list) {
if (Objects.equals(number, element)) {
list.remove(number);
}
}
}
static void removeWithIterator(List<Integer> list, int element) {
for (Iterator<Integer> i = list.iterator(); i.hasNext();) {
Integer number = i.next();
if (Objects.equals(number, element)) {
i.remove();
}
}
}
static List<Integer> removeWithCollectingAndReturningRemainingElements(List<Integer> list, int element) {
List<Integer> remainingElements = new ArrayList<>();
for (Integer number : list) {
if (!Objects.equals(number, element)) {
remainingElements.add(number);
}
}
return remainingElements;
}
static void removeWithCollectingRemainingElementsAndAddingToOriginalList(List<Integer> list, int element) {
List<Integer> remainingElements = new ArrayList<>();
for (Integer number : list) {
if (!Objects.equals(number, element)) {
remainingElements.add(number);
}
}
list.clear();
list.addAll(remainingElements);
}
static List<Integer> removeWithStreamFilter(List<Integer> list, Integer element) {
return list.stream()
.filter(e -> !Objects.equals(e, element))
.collect(Collectors.toList());
}
static void removeWithRemoveIf(List<Integer> list, Integer element) {
list.removeIf(n -> Objects.equals(n, element));
}
}

View File

@ -0,0 +1,208 @@
package com.baeldung.list;
import static com.baeldung.list.RemoveAll.removeWithCallingRemoveUntilModifies;
import static com.baeldung.list.RemoveAll.removeWithCollectingAndReturningRemainingElements;
import static com.baeldung.list.RemoveAll.removeWithCollectingRemainingElementsAndAddingToOriginalList;
import static com.baeldung.list.RemoveAll.removeWithForEachLoop;
import static com.baeldung.list.RemoveAll.removeWithForLoopDecrementOnRemove;
import static com.baeldung.list.RemoveAll.removeWithForLoopIncrementIfRemains;
import static com.baeldung.list.RemoveAll.removeWithIterator;
import static com.baeldung.list.RemoveAll.removeWithRemoveIf;
import static com.baeldung.list.RemoveAll.removeWithStandardForLoopUsingIndex;
import static com.baeldung.list.RemoveAll.removeWithStreamFilter;
import static com.baeldung.list.RemoveAll.*;
import static org.assertj.core.api.Assertions.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.List;
import org.junit.Test;
public class RemoveAllUnitTest {
private List<Integer> list(Integer... elements) {
return new ArrayList<>(Arrays.asList(elements));
}
@Test
public void givenAList_whenRemovingElementsWithWhileLoopUsingPrimitiveElement_thenTheResultCorrect() {
// given
List<Integer> list = list(1, 2, 3);
int valueToRemove = 1;
// when
assertThatThrownBy(() -> removeWithWhileLoopPrimitiveElement(list, valueToRemove))
.isInstanceOf(IndexOutOfBoundsException.class);
}
@Test
public void givenAList_whenRemovingElementsWithWhileLoopUsingNonPrimitiveElement_thenTheResultCorrect() {
// given
List<Integer> list = list(1, 2, 3);
int valueToRemove = 1;
// when
removeWithWhileLoopNonPrimitiveElement(list, valueToRemove);
// then
assertThat(list).isEqualTo(list(2, 3));
}
@Test
public void givenAList_whenRemovingElementsWithWhileLoopStoringFirstOccurrenceIndex_thenTheResultCorrect() {
// given
List<Integer> list = list(1, 2, 3);
int valueToRemove = 1;
// when
removeWithWhileLoopStoringFirstOccurrenceIndex(list, valueToRemove);
// then
assertThat(list).isEqualTo(list(2, 3));
}
@Test
public void givenAList_whenRemovingElementsWithCallingRemoveUntilModifies_thenTheResultIsCorrect() {
// given
List<Integer> list = list(1, 1, 2, 3);
int valueToRemove = 1;
// when
removeWithCallingRemoveUntilModifies(list, valueToRemove);
// then
assertThat(list).isEqualTo(list(2, 3));
}
@Test
public void givenAListWithoutDuplication_whenRemovingElementsWithStandardForLoopUsingIndex_thenTheResultIsCorrect() {
// given
List<Integer> list = list(1, 2, 3);
int valueToRemove = 1;
// when
removeWithStandardForLoopUsingIndex(list, valueToRemove);
// then
assertThat(list).isEqualTo(list(2, 3));
}
@Test
public void givenAListWithAdjacentElements_whenRemovingElementsWithStandardForLoop_thenTheResultIsInCorrect() {
// given
List<Integer> list = list(1, 1, 2, 3);
int valueToRemove = 1;
// when
removeWithStandardForLoopUsingIndex(list, valueToRemove);
// then
assertThat(list).isEqualTo(list(1, 2, 3));
}
@Test
public void givenAListWithAdjacentElements_whenRemovingElementsWithForLoopAndDecrementOnRemove_thenTheResultIsCorrect() {
// given
List<Integer> list = list(1, 1, 2, 3);
int valueToRemove = 1;
// when
removeWithForLoopDecrementOnRemove(list, valueToRemove);
// then
assertThat(list).isEqualTo(list(2, 3));
}
@Test
public void givenAListWithAdjacentElements_whenRemovingElementsWithForLoopAndIncrementIfRemains_thenTheResultIsCorrect() {
// given
List<Integer> list = list(1, 1, 2, 3);
int valueToRemove = 1;
// when
removeWithForLoopIncrementIfRemains(list, valueToRemove);
// then
assertThat(list).isEqualTo(list(2, 3));
}
@Test
public void givenAList_whenRemovingElementsWithForEachLoop_thenExceptionIsThrown() {
// given
List<Integer> list = list(1, 1, 2, 3);
int valueToRemove = 1;
// when
assertThatThrownBy(() -> removeWithForEachLoop(list, valueToRemove))
.isInstanceOf(ConcurrentModificationException.class);
}
@Test
public void givenAList_whenRemovingElementsWithIterator_thenTheResultIsCorrect() {
// given
List<Integer> list = list(1, 1, 2, 3);
int valueToRemove = 1;
// when
removeWithIterator(list, valueToRemove);
// then
assertThat(list).isEqualTo(list(2, 3));
}
@Test
public void givenAList_whenRemovingElementsWithCollectingAndReturningRemainingElements_thenTheResultIsCorrect() {
// given
List<Integer> list = list(1, 1, 2, 3);
int valueToRemove = 1;
// when
List<Integer> result = removeWithCollectingAndReturningRemainingElements(list, valueToRemove);
// then
assertThat(result).isEqualTo(list(2, 3));
}
@Test
public void givenAList_whenRemovingElementsWithCollectingRemainingAndAddingToOriginalList_thenTheResultIsCorrect() {
// given
List<Integer> list = list(1, 1, 2, 3);
int valueToRemove = 1;
// when
removeWithCollectingRemainingElementsAndAddingToOriginalList(list, valueToRemove);
// then
assertThat(list).isEqualTo(list(2, 3));
}
@Test
public void givenAList_whenRemovingElementsWithStreamFilter_thenTheResultIsCorrect() {
// given
List<Integer> list = list(1, 1, 2, 3);
int valueToRemove = 1;
// when
List<Integer> result = removeWithStreamFilter(list, valueToRemove);
// then
assertThat(result).isEqualTo(list(2, 3));
}
@Test
public void givenAList_whenRemovingElementsWithCallingRemoveIf_thenTheResultIsCorrect() {
// given
List<Integer> list = list(1, 1, 2, 3);
int valueToRemove = 1;
// when
removeWithRemoveIf(list, valueToRemove);
// then
assertThat(list).isEqualTo(list(2, 3));
}
}