From 9e5c656d84853bd4fccba8ca517a284d651068f2 Mon Sep 17 00:00:00 2001 From: Eugene Kovko <37694937+eukovko@users.noreply.github.com> Date: Fri, 29 Sep 2023 23:39:41 +0200 Subject: [PATCH] BAEL-6781: Skip First Iteration in Java Foreach (#14876) --- .../SkipFirstElementExample.java | 126 ++++++++++++++++++ .../SkipFirstElementExampleUnitTest.java | 122 +++++++++++++++++ .../skippingfirstelement/TestableSkip.java | 10 ++ .../TestableSkipFirstElement.java | 37 +++++ 4 files changed, 295 insertions(+) create mode 100644 core-java-modules/core-java-collections-5/src/main/java/com/baeldung/skippingfirstelement/SkipFirstElementExample.java create mode 100644 core-java-modules/core-java-collections-5/src/test/java/com/baeldung/skippingfirstelement/SkipFirstElementExampleUnitTest.java create mode 100644 core-java-modules/core-java-collections-5/src/test/java/com/baeldung/skippingfirstelement/TestableSkip.java create mode 100644 core-java-modules/core-java-collections-5/src/test/java/com/baeldung/skippingfirstelement/TestableSkipFirstElement.java diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/skippingfirstelement/SkipFirstElementExample.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/skippingfirstelement/SkipFirstElementExample.java new file mode 100644 index 0000000000..86982486fa --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/skippingfirstelement/SkipFirstElementExample.java @@ -0,0 +1,126 @@ +package com.baeldung.skippingfirstelement; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +public class SkipFirstElementExample { + + private final List stringList = new ArrayList<>(); + private final Set stringSet = new HashSet<>(); + private final Map stringMap = new HashMap<>(); + + public SkipFirstElementExample() { + // Initializing a List + stringList.add("Monday"); + stringList.add("Tuesday"); + stringList.add("Wednesday"); + stringList.add("Thursday"); + stringList.add("Friday"); + stringList.add("Saturday"); + stringList.add("Sunday"); + // Initializing a Set + stringSet.add("Monday"); + stringSet.add("Tuesday"); + stringSet.add("Wednesday"); + stringSet.add("Thursday"); + stringSet.add("Friday"); + stringSet.add("Saturday"); + stringSet.add("Sunday"); + // Initializing a Map + stringMap.put("Monday", "The day when coffee is a life support system."); + stringMap.put("Tuesday", "The day you realize that Monday's optimism was a lie."); + stringMap.put("Wednesday", "Hump Day, or as it's known, the 'Is it Friday yet?' day."); + stringMap.put("Thursday", "The day that says, 'Hold my beer, Friday is coming!'"); + stringMap.put("Friday", "The golden child of the weekdays. The superhero of the workweek."); + stringMap.put("Saturday", "The day of rest? More like the day of 'What can I binge-watch next?'"); + stringMap.put("Sunday", "The day before you have to adult again."); + } + + void skippingFirstElementInListWithForLoop(List stringList) { + for (int i = 1; i < stringList.size(); i++) { + process(stringList.get(i)); + } + } + + void skippingFirstElementInListWithWhileLoop(List stringList) { + final Iterator iterator = stringList.iterator(); + if (iterator.hasNext()) { + iterator.next(); + } + while (iterator.hasNext()) { + process(iterator.next()); + } + } + + void skippingFirstElementInSetWithWhileLoop(Set stringSet) { + final Iterator iterator = stringSet.iterator(); + if (iterator.hasNext()) { + iterator.next(); + } + while (iterator.hasNext()) { + process(iterator.next()); + } + } + + void skippingFirstElementInListWithWhileLoopStoringFirstElement(List stringList) { + final Iterator iterator = stringList.iterator(); + String firstElement = null; + if (iterator.hasNext()) { + firstElement = iterator.next(); + } + while (iterator.hasNext()) { + process(iterator.next()); + // additional logic using fistElement + } + } + + void skippingFirstElementInMapWithStreamSkip(Map stringMap) { + stringMap.entrySet().stream().skip(1).forEach(this::process); + } + + void skippingFirstElementInListWithSubList(List stringList) { + for (final String element : stringList.subList(1, stringList.size())) { + process(element); + } + } + + void skippingFirstElementInListWithForLoopWithAdditionalCheck(List stringList) { + for (int i = 0; i < stringList.size(); i++) { + if (i == 0) { + // do something else + } else { + process(stringList.get(i)); + } + } + } + + void skippingFirstElementInListWithWhileLoopWithCounter(List stringList) { + int counter = 0; + while (counter < stringList.size()) { + if (counter != 0) { + process(stringList.get(counter)); + } + ++counter; + } + } + + void skippingFirstElementInListWithReduce(List stringList) { + stringList.stream().reduce((skip, element) -> { + process(element); + return element; + }); + } + + protected void process(String string) { + System.out.println(string); + } + protected void process(Entry mapEntry) { + System.out.println(mapEntry); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/skippingfirstelement/SkipFirstElementExampleUnitTest.java b/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/skippingfirstelement/SkipFirstElementExampleUnitTest.java new file mode 100644 index 0000000000..9821b22ac7 --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/skippingfirstelement/SkipFirstElementExampleUnitTest.java @@ -0,0 +1,122 @@ +package com.baeldung.skippingfirstelement; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Stream; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +@EnabledForJreRange(min = JRE.JAVA_9) +class SkipFirstElementExampleUnitTest { + + private static TestableSkipFirstElement testableSkip = new TestableSkipFirstElement(); + + @BeforeEach + void setup() { + testableSkip.reset(); + } + + private static Stream listProvider() { + return Stream.of( + Arguments.of( + List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"), + List.of("Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")) + ); + } + + private static Stream mapProvider() { + return Stream.of( + Arguments.of( + Map.of( + "Monday", "The day when coffee is a life support system.", + "Tuesday", "The day you realize that Monday's optimism was a lie.", + "Wednesday", "Hump Day, or as it's known, the 'Is it Friday yet?' day.", + "Thursday", "The day that says, 'Hold my beer, Friday is coming!'", + "Friday", "The golden child of the weekdays. The superhero of the workweek.", + "Saturday", "The day of rest? More like the day of 'What can I binge-watch next?'", + "Sunday", "The day before you have to adult again." + ) + ) + ); + } + + @ParameterizedTest + @MethodSource("listProvider") + void skippingFirstElementInListWithForLoop(List input, List expected) { + testableSkip.skippingFirstElementInListWithForLoop(input); + List actual = testableSkip.getResult(); + assertEquals(actual, expected); + } + + @ParameterizedTest + @MethodSource("listProvider") + void skippingFirstElementInListWithWhileLoop(List input, List expected) { + testableSkip.skippingFirstElementInListWithWhileLoop(input); + List actual = testableSkip.getResult(); + assertEquals(actual, expected); + } + + @ParameterizedTest + @MethodSource("listProvider") + void skippingFirstElementInSetWithWhileLoop(List input) { + testableSkip.skippingFirstElementInSetWithWhileLoop(new HashSet<>(input)); + Set actual = new HashSet<>(testableSkip.getResult()); + assertEquals(actual.size(), input.size() - 1); + } + + @ParameterizedTest + @MethodSource("listProvider") + void skippingFirstElementInListWithWhileLoopStoringFirstElement(List input, List expected) { + testableSkip.skippingFirstElementInListWithWhileLoopStoringFirstElement(input); + List actual = testableSkip.getResult(); + assertEquals(actual, expected); + } + + @ParameterizedTest + @MethodSource("mapProvider") + void skippingFirstElementInMapWithStreamSkip(Map input) { + testableSkip.skippingFirstElementInMapWithStreamSkip(input); + List actual = testableSkip.getResult(); + assertEquals(actual.size(), input.size() - 1); + } + + @ParameterizedTest + @MethodSource("listProvider") + void skippingFirstElementInListWithSubList(List input, List expected) { + testableSkip.skippingFirstElementInListWithSubList(input); + List actual = testableSkip.getResult(); + assertEquals(actual, expected); + } + + @ParameterizedTest + @MethodSource("listProvider") + void skippingFirstElementInListWithForLoopWithAdditionalCheck(List input, List expected) { + testableSkip.skippingFirstElementInListWithForLoopWithAdditionalCheck(input); + List actual = testableSkip.getResult(); + assertEquals(actual, expected); + } + + @ParameterizedTest + @MethodSource("listProvider") + void skippingFirstElementInListWithWhileLoopWithCounter(List input, List expected) { + testableSkip.skippingFirstElementInListWithWhileLoopWithCounter(input); + List actual = testableSkip.getResult(); + assertEquals(actual, expected); + } + + @ParameterizedTest + @MethodSource("listProvider") + void skippingFirstElementInListWithReduce(List input, List expected) { + testableSkip.skippingFirstElementInListWithReduce(input); + List actual = testableSkip.getResult(); + assertEquals(actual, expected); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/skippingfirstelement/TestableSkip.java b/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/skippingfirstelement/TestableSkip.java new file mode 100644 index 0000000000..0e2f340485 --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/skippingfirstelement/TestableSkip.java @@ -0,0 +1,10 @@ +package com.baeldung.skippingfirstelement; + +import java.util.List; + +public interface TestableSkip { + + void reset(); + + List getResult(); +} diff --git a/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/skippingfirstelement/TestableSkipFirstElement.java b/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/skippingfirstelement/TestableSkipFirstElement.java new file mode 100644 index 0000000000..99facb73ad --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/skippingfirstelement/TestableSkipFirstElement.java @@ -0,0 +1,37 @@ +package com.baeldung.skippingfirstelement; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map.Entry; + +public class TestableSkipFirstElement extends SkipFirstElementExample implements TestableSkip { + + + private List processedList = new ArrayList<>(); + private List> processedEntryList = new ArrayList<>(); + + @Override + public void process(String string) { + processedList.add(string); + } + + @Override + public void process(Entry stringEntry) { + processedEntryList.add(stringEntry); + } + + @Override + public void reset() { + processedList.clear(); + processedEntryList.clear(); + } + + @Override + public List getResult() { + if (!processedList.isEmpty()) + return processedList; + return processedEntryList; + } + + +}