diff --git a/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/iterationcounter/IterationCounter.java b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/iterationcounter/IterationCounter.java new file mode 100644 index 0000000000..40d997cb0e --- /dev/null +++ b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/iterationcounter/IterationCounter.java @@ -0,0 +1,68 @@ +package com.baeldung.iterationcounter; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.stream.Stream; + +public class IterationCounter { + public static final List IMDB_TOP_MOVIES = Arrays.asList("The Shawshank Redemption", + "The Godfather", "The Godfather II", "The Dark Knight"); + + public static List getRankingsWithForLoop(List movies) { + List rankings = new ArrayList<>(); + for (int i = 0; i < movies.size(); i++) { + String ranking = (i + 1) + ": " + movies.get(i); + rankings.add(ranking); + } + return rankings; + } + + public static List getRankingsWithForEachLoop(List movies) { + List rankings = new ArrayList<>(); + int i = 0; + for (String movie : movies) { + String ranking = (i + 1) + ": " + movies.get(i); + rankings.add(ranking); + + i++; + } + return rankings; + } + + public static List getRankingsWithFunctionalForEachLoop(List movies) { + List rankings = new ArrayList<>(); + forEachWithCounter(movies, (i, movie) -> { + String ranking = (i + 1) + ": " + movie; + rankings.add(ranking); + }); + + return rankings; + } + + public static void forEachWithCounter(Iterable source, BiConsumer consumer) { + int i = 0; + for (T item : source) { + consumer.accept(i, item); + i++; + } + } + + public static List getRankingsWithStream(Stream movies) { + List rankings = new ArrayList<>(); + movies.forEach(withCounter((i, movie) -> { + String ranking = (i + 1) + ": " + movie; + rankings.add(ranking); + })); + + return rankings; + } + + public static Consumer withCounter(BiConsumer consumer) { + AtomicInteger counter = new AtomicInteger(0); + return item -> consumer.accept(counter.getAndIncrement(), item); + } +} diff --git a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/iterationcounter/IterationCounterUnitTest.java b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/iterationcounter/IterationCounterUnitTest.java new file mode 100644 index 0000000000..6746e570ca --- /dev/null +++ b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/iterationcounter/IterationCounterUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.iterationcounter; + +import org.junit.Test; + +import static com.baeldung.iterationcounter.IterationCounter.*; +import static org.assertj.core.api.Assertions.*; + +public class IterationCounterUnitTest { + @Test + public void givenRankings_whenCalculateWithForLoop_thenRankingsCorrect() { + assertThat(getRankingsWithForLoop(IMDB_TOP_MOVIES)) + .containsExactly("1: The Shawshank Redemption", + "2: The Godfather", "3: The Godfather II", "4: The Dark Knight"); + } + + @Test + public void givenRankings_whenCalculateWithForEachLoop_thenRankingsCorrect() { + assertThat(getRankingsWithForEachLoop(IMDB_TOP_MOVIES)) + .containsExactly("1: The Shawshank Redemption", + "2: The Godfather", "3: The Godfather II", "4: The Dark Knight"); + } + + @Test + public void givenRankings_whenCalculateWithFunctionalForEach_thenRankingsCorrect() { + assertThat(getRankingsWithFunctionalForEachLoop(IMDB_TOP_MOVIES)) + .containsExactly("1: The Shawshank Redemption", + "2: The Godfather", "3: The Godfather II", "4: The Dark Knight"); + } + + @Test + public void givenRankings_whenCalculateWithStream_thenRankingsCorrect() { + assertThat(getRankingsWithStream(IMDB_TOP_MOVIES.stream())) + .containsExactly("1: The Shawshank Redemption", + "2: The Godfather", "3: The Godfather II", "4: The Dark Knight"); + } +}