From 1864b8e90d28705e09128ddfed2eb23adbf2598e Mon Sep 17 00:00:00 2001 From: hajk1 Date: Wed, 3 Apr 2024 01:32:44 +0330 Subject: [PATCH 1/3] adding new article BAEL-7497 source codes --- .../core-java-collections-6/README.md | 7 ++ .../core-java-collections-6/pom.xml | 54 +++++++++++++++ .../IteratorVsForeachUnitTest.java | 69 +++++++++++++++++++ core-java-modules/pom.xml | 1 + 4 files changed, 131 insertions(+) create mode 100644 core-java-modules/core-java-collections-6/README.md create mode 100644 core-java-modules/core-java-collections-6/pom.xml create mode 100644 core-java-modules/core-java-collections-6/src/test/java/com/baeldung/iteratorvsforeach/IteratorVsForeachUnitTest.java diff --git a/core-java-modules/core-java-collections-6/README.md b/core-java-modules/core-java-collections-6/README.md new file mode 100644 index 0000000000..736e91f110 --- /dev/null +++ b/core-java-modules/core-java-collections-6/README.md @@ -0,0 +1,7 @@ +========= + +## Core Java Collections Cookbooks and Examples + +### Relevant Articles: + +- More articles: [[<-- prev]](/core-java-modules/core-java-collections-5) diff --git a/core-java-modules/core-java-collections-6/pom.xml b/core-java-modules/core-java-collections-6/pom.xml new file mode 100644 index 0000000000..8294a4d76a --- /dev/null +++ b/core-java-modules/core-java-collections-6/pom.xml @@ -0,0 +1,54 @@ + + + core-java-collections-6 + + + + maven-compiler-plugin + + 9 + 9 + + org.apache.maven.plugins + + + + + + junit-platform-runner + org.junit.platform + test + ${junit-platform.version} + + + junit-jupiter + org.junit.jupiter + test + ${junit.version} + + + junit-vintage-engine + org.junit.vintage + test + ${junit.version} + + + 4.0.0 + + core-java-collections-6 + + jar + + + core-java-modules + com.baeldung.core-java-modules + 0.0.1-SNAPSHOT + + + + 5.10.2 + + + 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 new file mode 100644 index 0000000000..a95c1a1e98 --- /dev/null +++ b/core-java-modules/core-java-collections-6/src/test/java/com/baeldung/iteratorvsforeach/IteratorVsForeachUnitTest.java @@ -0,0 +1,69 @@ +package com.baeldung.iteratorvsforeach; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertIterableEquals; + +import java.util.ArrayList; +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; +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 + } + } + assertIterableEquals(expected, mutableList); + } + +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 3d26d51a6a..56360d0106 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -89,6 +89,7 @@ core-java-collections-3 core-java-collections-4 core-java-collections-5 + core-java-collections-6 core-java-collections-conversions core-java-collections-set-2 core-java-collections-list From fc31ffd062fabb34622debdfd7f1bde4d963b395 Mon Sep 17 00:00:00 2001 From: hajk1 Date: Fri, 5 Apr 2024 00:41:35 +0330 Subject: [PATCH 2/3] reformat the pom file to be more logical --- .../core-java-collections-6/pom.xml | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/core-java-modules/core-java-collections-6/pom.xml b/core-java-modules/core-java-collections-6/pom.xml index 8294a4d76a..ae95ecac07 100644 --- a/core-java-modules/core-java-collections-6/pom.xml +++ b/core-java-modules/core-java-collections-6/pom.xml @@ -2,19 +2,17 @@ + 4.0.0 core-java-collections-6 - - - - maven-compiler-plugin - - 9 - 9 - - org.apache.maven.plugins - - - + core-java-collections-6 + jar + + + core-java-modules + com.baeldung.core-java-modules + 0.0.1-SNAPSHOT + + junit-platform-runner @@ -35,17 +33,19 @@ ${junit.version} - 4.0.0 - core-java-collections-6 - - jar - - - core-java-modules - com.baeldung.core-java-modules - 0.0.1-SNAPSHOT - + + + + maven-compiler-plugin + + 9 + 9 + + org.apache.maven.plugins + + + 5.10.2 From 2a754ad3372b730792eab64e3b71cd1053731d27 Mon Sep 17 00:00:00 2001 From: hajk1 Date: Sun, 7 Apr 2024 18:25:13 +0330 Subject: [PATCH 3/3] reformat the files using intellij-baeldung-style.xml --- .../core-java-collections-6/pom.xml | 92 +++++++++--------- .../IteratorVsForeachUnitTest.java | 93 +++++++++---------- 2 files changed, 90 insertions(+), 95 deletions(-) diff --git a/core-java-modules/core-java-collections-6/pom.xml b/core-java-modules/core-java-collections-6/pom.xml index ae95ecac07..5140947479 100644 --- a/core-java-modules/core-java-collections-6/pom.xml +++ b/core-java-modules/core-java-collections-6/pom.xml @@ -1,54 +1,54 @@ - 4.0.0 - core-java-collections-6 - core-java-collections-6 - jar + xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + core-java-collections-6 + core-java-collections-6 + jar - - core-java-modules - com.baeldung.core-java-modules - 0.0.1-SNAPSHOT - + + core-java-modules + com.baeldung.core-java-modules + 0.0.1-SNAPSHOT + - - - junit-platform-runner - org.junit.platform - test - ${junit-platform.version} - - - junit-jupiter - org.junit.jupiter - test - ${junit.version} - - - junit-vintage-engine - org.junit.vintage - test - ${junit.version} - - + + + junit-platform-runner + org.junit.platform + test + ${junit-platform.version} + + + junit-jupiter + org.junit.jupiter + test + ${junit.version} + + + junit-vintage-engine + org.junit.vintage + test + ${junit.version} + + - - - - maven-compiler-plugin - - 9 - 9 - - org.apache.maven.plugins - - - + + + + maven-compiler-plugin + + 9 + 9 + + org.apache.maven.plugins + + + - - 5.10.2 - + + 5.10.2 + 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); - } }