From a1e28048a67da7eedbef659085a6fb8850bbd98a Mon Sep 17 00:00:00 2001 From: hajk1 Date: Sun, 7 Apr 2024 18:25:13 +0330 Subject: [PATCH] reformat the files using intellij-baeldung-style.xml --- .../IteratorVsForeachUnitTest.java | 93 +++++++++---------- 1 file changed, 44 insertions(+), 49 deletions(-) diff --git a/core-java-modules/core-java-collections-6/src/test/java/com/baeldung/iteratorvsforeach/IteratorVsForeachUnitTest.java b/core-java-modules/core-java-collections-6/src/test/java/com/baeldung/iteratorvsforeach/IteratorVsForeachUnitTest.java index a95c1a1e98..ca9390a3ee 100644 --- a/core-java-modules/core-java-collections-6/src/test/java/com/baeldung/iteratorvsforeach/IteratorVsForeachUnitTest.java +++ b/core-java-modules/core-java-collections-6/src/test/java/com/baeldung/iteratorvsforeach/IteratorVsForeachUnitTest.java @@ -8,6 +8,7 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.stream.Stream; + import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -15,55 +16,49 @@ import org.junit.jupiter.params.provider.MethodSource; class IteratorVsForeachUnitTest { - private static Stream listProvider() { - return Stream.of( - Arguments.of( - List.of("String1", "String2", "unwanted"), - List.of("String1", "String2")) - ); - } - - @Test - public void givenEmptyCollection_whenUsingForEach_thenNoElementsAreIterated() { - List names = Collections.emptyList(); - StringBuilder stringBuilder = new StringBuilder(); - names.forEach(stringBuilder::append); - assertEquals("", stringBuilder.toString()); - } - - @ParameterizedTest - @MethodSource("listProvider") - public void givenCollectionWithElements_whenRemovingElementDuringForEachIteration_thenElementIsRemoved( - List input, List expected) { - List mutableList = new ArrayList<>(input); - // Separate collection for items to be removed - List toRemove = new ArrayList<>(); - - // Using forEach to identify items to remove - input.forEach(item -> { - if (item.equals("unwanted")) { - toRemove.add(item); - } - }); - - // Removing the identified items from the original list - mutableList.removeAll(toRemove); - assertIterableEquals(expected, mutableList); - } - - @ParameterizedTest - @MethodSource("listProvider") - public void givenCollectionWithElements_whenRemovingElementDuringIteratorIteration_thenElementIsRemoved( - List input, List expected) { - List mutableList = new ArrayList<>(input); - Iterator it = mutableList.iterator(); - while (it.hasNext()) { - String item = it.next(); - if (item.equals("unwanted")) { - it.remove(); // Safely remove item - } + private static Stream listProvider() { + return Stream.of(Arguments.of(List.of("String1", "String2", "unwanted"), List.of("String1", "String2"))); + } + + @Test + public void givenEmptyCollection_whenUsingForEach_thenNoElementsAreIterated() { + List names = Collections.emptyList(); + StringBuilder stringBuilder = new StringBuilder(); + names.forEach(stringBuilder::append); + assertEquals("", stringBuilder.toString()); + } + + @ParameterizedTest + @MethodSource("listProvider") + public void givenCollectionWithElements_whenRemovingElementDuringForEachIteration_thenElementIsRemoved(List input, List expected) { + List mutableList = new ArrayList<>(input); + // Separate collection for items to be removed + List toRemove = new ArrayList<>(); + + // Using forEach to identify items to remove + input.forEach(item -> { + if (item.equals("unwanted")) { + toRemove.add(item); + } + }); + + // Removing the identified items from the original list + mutableList.removeAll(toRemove); + assertIterableEquals(expected, mutableList); + } + + @ParameterizedTest + @MethodSource("listProvider") + public void givenCollectionWithElements_whenRemovingElementDuringIteratorIteration_thenElementIsRemoved(List input, List expected) { + List mutableList = new ArrayList<>(input); + Iterator it = mutableList.iterator(); + while (it.hasNext()) { + String item = it.next(); + if (item.equals("unwanted")) { + it.remove(); // Safely remove item + } + } + assertIterableEquals(expected, mutableList); } - assertIterableEquals(expected, mutableList); - } }