BAEL-6781: Skip First Iteration in Java Foreach (#14876)

This commit is contained in:
Eugene Kovko 2023-09-29 23:39:41 +02:00 committed by GitHub
parent d00863eeb2
commit 9e5c656d84
4 changed files with 295 additions and 0 deletions

View File

@ -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<String> stringList = new ArrayList<>();
private final Set<String> stringSet = new HashSet<>();
private final Map<String, String> 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<String> stringList) {
for (int i = 1; i < stringList.size(); i++) {
process(stringList.get(i));
}
}
void skippingFirstElementInListWithWhileLoop(List<String> stringList) {
final Iterator<String> iterator = stringList.iterator();
if (iterator.hasNext()) {
iterator.next();
}
while (iterator.hasNext()) {
process(iterator.next());
}
}
void skippingFirstElementInSetWithWhileLoop(Set<String> stringSet) {
final Iterator<String> iterator = stringSet.iterator();
if (iterator.hasNext()) {
iterator.next();
}
while (iterator.hasNext()) {
process(iterator.next());
}
}
void skippingFirstElementInListWithWhileLoopStoringFirstElement(List<String> stringList) {
final Iterator<String> 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<String, String> stringMap) {
stringMap.entrySet().stream().skip(1).forEach(this::process);
}
void skippingFirstElementInListWithSubList(List<String> stringList) {
for (final String element : stringList.subList(1, stringList.size())) {
process(element);
}
}
void skippingFirstElementInListWithForLoopWithAdditionalCheck(List<String> stringList) {
for (int i = 0; i < stringList.size(); i++) {
if (i == 0) {
// do something else
} else {
process(stringList.get(i));
}
}
}
void skippingFirstElementInListWithWhileLoopWithCounter(List<String> stringList) {
int counter = 0;
while (counter < stringList.size()) {
if (counter != 0) {
process(stringList.get(counter));
}
++counter;
}
}
void skippingFirstElementInListWithReduce(List<String> stringList) {
stringList.stream().reduce((skip, element) -> {
process(element);
return element;
});
}
protected void process(String string) {
System.out.println(string);
}
protected void process(Entry<String, String> mapEntry) {
System.out.println(mapEntry);
}
}

View File

@ -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<Arguments> 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<Arguments> 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<String> input, List<String> expected) {
testableSkip.skippingFirstElementInListWithForLoop(input);
List<?> actual = testableSkip.getResult();
assertEquals(actual, expected);
}
@ParameterizedTest
@MethodSource("listProvider")
void skippingFirstElementInListWithWhileLoop(List<String> input, List<String> expected) {
testableSkip.skippingFirstElementInListWithWhileLoop(input);
List<?> actual = testableSkip.getResult();
assertEquals(actual, expected);
}
@ParameterizedTest
@MethodSource("listProvider")
void skippingFirstElementInSetWithWhileLoop(List<String> input) {
testableSkip.skippingFirstElementInSetWithWhileLoop(new HashSet<>(input));
Set<?> actual = new HashSet<>(testableSkip.getResult());
assertEquals(actual.size(), input.size() - 1);
}
@ParameterizedTest
@MethodSource("listProvider")
void skippingFirstElementInListWithWhileLoopStoringFirstElement(List<String> input, List<String> expected) {
testableSkip.skippingFirstElementInListWithWhileLoopStoringFirstElement(input);
List<?> actual = testableSkip.getResult();
assertEquals(actual, expected);
}
@ParameterizedTest
@MethodSource("mapProvider")
void skippingFirstElementInMapWithStreamSkip(Map<String, String> input) {
testableSkip.skippingFirstElementInMapWithStreamSkip(input);
List<?> actual = testableSkip.getResult();
assertEquals(actual.size(), input.size() - 1);
}
@ParameterizedTest
@MethodSource("listProvider")
void skippingFirstElementInListWithSubList(List<String> input, List<String> expected) {
testableSkip.skippingFirstElementInListWithSubList(input);
List<?> actual = testableSkip.getResult();
assertEquals(actual, expected);
}
@ParameterizedTest
@MethodSource("listProvider")
void skippingFirstElementInListWithForLoopWithAdditionalCheck(List<String> input, List<String> expected) {
testableSkip.skippingFirstElementInListWithForLoopWithAdditionalCheck(input);
List<?> actual = testableSkip.getResult();
assertEquals(actual, expected);
}
@ParameterizedTest
@MethodSource("listProvider")
void skippingFirstElementInListWithWhileLoopWithCounter(List<String> input, List<String> expected) {
testableSkip.skippingFirstElementInListWithWhileLoopWithCounter(input);
List<?> actual = testableSkip.getResult();
assertEquals(actual, expected);
}
@ParameterizedTest
@MethodSource("listProvider")
void skippingFirstElementInListWithReduce(List<String> input, List<String> expected) {
testableSkip.skippingFirstElementInListWithReduce(input);
List<?> actual = testableSkip.getResult();
assertEquals(actual, expected);
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.skippingfirstelement;
import java.util.List;
public interface TestableSkip {
void reset();
List<?> getResult();
}

View File

@ -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<String> processedList = new ArrayList<>();
private List<Entry<String, String>> processedEntryList = new ArrayList<>();
@Override
public void process(String string) {
processedList.add(string);
}
@Override
public void process(Entry<String, String> stringEntry) {
processedEntryList.add(stringEntry);
}
@Override
public void reset() {
processedList.clear();
processedEntryList.clear();
}
@Override
public List<?> getResult() {
if (!processedList.isEmpty())
return processedList;
return processedEntryList;
}
}