From cc4ab484ccd0514aa3426ac3566f27fde945fbe5 Mon Sep 17 00:00:00 2001 From: fanatixan Date: Thu, 2 Aug 2018 08:59:55 +0200 Subject: [PATCH] moved examples for 'removing all occurrences of an element from a list' to core-java-collections (#4878) --- .../com/baeldung/list/RemoveAllUnitTest.java | 208 ----------------- .../baeldung/list/removeall}/RemoveAll.java | 2 +- .../list/removeall/RemoveAllUnitTest.java | 210 ++++++++++++++++++ 3 files changed, 211 insertions(+), 209 deletions(-) delete mode 100644 core-java-8/src/test/java/com/baeldung/list/RemoveAllUnitTest.java rename {core-java-8/src/main/java/com/baeldung/list => core-java-collections/src/main/java/com/baeldung/list/removeall}/RemoveAll.java (98%) create mode 100644 core-java-collections/src/test/java/com/baeldung/list/removeall/RemoveAllUnitTest.java diff --git a/core-java-8/src/test/java/com/baeldung/list/RemoveAllUnitTest.java b/core-java-8/src/test/java/com/baeldung/list/RemoveAllUnitTest.java deleted file mode 100644 index 7ada66a49e..0000000000 --- a/core-java-8/src/test/java/com/baeldung/list/RemoveAllUnitTest.java +++ /dev/null @@ -1,208 +0,0 @@ -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 list(Integer... elements) { - return new ArrayList<>(Arrays.asList(elements)); - } - - @Test - public void givenAList_whenRemovingElementsWithWhileLoopUsingPrimitiveElement_thenTheResultCorrect() { - // given - List list = list(1, 2, 3); - int valueToRemove = 1; - - // when - assertThatThrownBy(() -> removeWithWhileLoopPrimitiveElement(list, valueToRemove)) - .isInstanceOf(IndexOutOfBoundsException.class); - } - - @Test - public void givenAList_whenRemovingElementsWithWhileLoopUsingNonPrimitiveElement_thenTheResultCorrect() { - // given - List 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 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 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 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 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 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 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 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 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 list = list(1, 1, 2, 3); - int valueToRemove = 1; - - // when - List result = removeWithCollectingAndReturningRemainingElements(list, valueToRemove); - - // then - assertThat(result).isEqualTo(list(2, 3)); - } - - @Test - public void givenAList_whenRemovingElementsWithCollectingRemainingAndAddingToOriginalList_thenTheResultIsCorrect() { - // given - List 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 list = list(1, 1, 2, 3); - int valueToRemove = 1; - - // when - List result = removeWithStreamFilter(list, valueToRemove); - - // then - assertThat(result).isEqualTo(list(2, 3)); - } - - @Test - public void givenAList_whenRemovingElementsWithCallingRemoveIf_thenTheResultIsCorrect() { - // given - List list = list(1, 1, 2, 3); - int valueToRemove = 1; - - // when - removeWithRemoveIf(list, valueToRemove); - - // then - assertThat(list).isEqualTo(list(2, 3)); - } - -} diff --git a/core-java-8/src/main/java/com/baeldung/list/RemoveAll.java b/core-java-collections/src/main/java/com/baeldung/list/removeall/RemoveAll.java similarity index 98% rename from core-java-8/src/main/java/com/baeldung/list/RemoveAll.java rename to core-java-collections/src/main/java/com/baeldung/list/removeall/RemoveAll.java index 0dd97af6e8..d5f9cf4b4e 100644 --- a/core-java-8/src/main/java/com/baeldung/list/RemoveAll.java +++ b/core-java-collections/src/main/java/com/baeldung/list/removeall/RemoveAll.java @@ -1,4 +1,4 @@ -package com.baeldung.list; +package com.baeldung.list.removeall; import java.util.ArrayList; import java.util.Iterator; diff --git a/core-java-collections/src/test/java/com/baeldung/list/removeall/RemoveAllUnitTest.java b/core-java-collections/src/test/java/com/baeldung/list/removeall/RemoveAllUnitTest.java new file mode 100644 index 0000000000..c23121053b --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/list/removeall/RemoveAllUnitTest.java @@ -0,0 +1,210 @@ +package com.baeldung.list.removeall; + +import static com.baeldung.list.removeall.RemoveAll.removeWithCallingRemoveUntilModifies; +import static com.baeldung.list.removeall.RemoveAll.removeWithCollectingAndReturningRemainingElements; +import static com.baeldung.list.removeall.RemoveAll.removeWithCollectingRemainingElementsAndAddingToOriginalList; +import static com.baeldung.list.removeall.RemoveAll.removeWithForEachLoop; +import static com.baeldung.list.removeall.RemoveAll.removeWithForLoopDecrementOnRemove; +import static com.baeldung.list.removeall.RemoveAll.removeWithForLoopIncrementIfRemains; +import static com.baeldung.list.removeall.RemoveAll.removeWithIterator; +import static com.baeldung.list.removeall.RemoveAll.removeWithRemoveIf; +import static com.baeldung.list.removeall.RemoveAll.removeWithStandardForLoopUsingIndex; +import static com.baeldung.list.removeall.RemoveAll.removeWithStreamFilter; +import static com.baeldung.list.removeall.RemoveAll.removeWithWhileLoopNonPrimitiveElement; +import static com.baeldung.list.removeall.RemoveAll.removeWithWhileLoopPrimitiveElement; +import static com.baeldung.list.removeall.RemoveAll.removeWithWhileLoopStoringFirstOccurrenceIndex; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +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 list(Integer... elements) { + return new ArrayList<>(Arrays.asList(elements)); + } + + @Test + public void givenAList_whenRemovingElementsWithWhileLoopUsingPrimitiveElement_thenTheResultCorrect() { + // given + List list = list(1, 2, 3); + int valueToRemove = 1; + + // when + assertThatThrownBy(() -> removeWithWhileLoopPrimitiveElement(list, valueToRemove)) + .isInstanceOf(IndexOutOfBoundsException.class); + } + + @Test + public void givenAList_whenRemovingElementsWithWhileLoopUsingNonPrimitiveElement_thenTheResultCorrect() { + // given + List 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 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 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 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 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 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 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 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 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 list = list(1, 1, 2, 3); + int valueToRemove = 1; + + // when + List result = removeWithCollectingAndReturningRemainingElements(list, valueToRemove); + + // then + assertThat(result).isEqualTo(list(2, 3)); + } + + @Test + public void givenAList_whenRemovingElementsWithCollectingRemainingAndAddingToOriginalList_thenTheResultIsCorrect() { + // given + List 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 list = list(1, 1, 2, 3); + int valueToRemove = 1; + + // when + List result = removeWithStreamFilter(list, valueToRemove); + + // then + assertThat(result).isEqualTo(list(2, 3)); + } + + @Test + public void givenAList_whenRemovingElementsWithCallingRemoveIf_thenTheResultIsCorrect() { + // given + List list = list(1, 1, 2, 3); + int valueToRemove = 1; + + // when + removeWithRemoveIf(list, valueToRemove); + + // then + assertThat(list).isEqualTo(list(2, 3)); + } + +}