Skip to content

Commit baa1609

Browse files
authored
BAEL-4560 Add various patterns for adding iteration counter to loops/streams (eugenp#10266)
* Add various patterns for adding iteration counter to loops/streams * Add various patterns for adding iteration counter to loops/streams
1 parent 5bdb487 commit baa1609

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.iterationcounter;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
import java.util.function.BiConsumer;
8+
import java.util.function.Consumer;
9+
import java.util.stream.Stream;
10+
11+
public class IterationCounter {
12+
public static final List<String> IMDB_TOP_MOVIES = Arrays.asList("The Shawshank Redemption",
13+
"The Godfather", "The Godfather II", "The Dark Knight");
14+
15+
public static List<String> getRankingsWithForLoop(List<String> movies) {
16+
List<String> rankings = new ArrayList<>();
17+
for (int i = 0; i < movies.size(); i++) {
18+
String ranking = (i + 1) + ": " + movies.get(i);
19+
rankings.add(ranking);
20+
}
21+
return rankings;
22+
}
23+
24+
public static List<String> getRankingsWithForEachLoop(List<String> movies) {
25+
List<String> rankings = new ArrayList<>();
26+
int i = 0;
27+
for (String movie : movies) {
28+
String ranking = (i + 1) + ": " + movies.get(i);
29+
rankings.add(ranking);
30+
31+
i++;
32+
}
33+
return rankings;
34+
}
35+
36+
public static List<String> getRankingsWithFunctionalForEachLoop(List<String> movies) {
37+
List<String> rankings = new ArrayList<>();
38+
forEachWithCounter(movies, (i, movie) -> {
39+
String ranking = (i + 1) + ": " + movie;
40+
rankings.add(ranking);
41+
});
42+
43+
return rankings;
44+
}
45+
46+
public static <T> void forEachWithCounter(Iterable<T> source, BiConsumer<Integer, T> consumer) {
47+
int i = 0;
48+
for (T item : source) {
49+
consumer.accept(i, item);
50+
i++;
51+
}
52+
}
53+
54+
public static List<String> getRankingsWithStream(Stream<String> movies) {
55+
List<String> rankings = new ArrayList<>();
56+
movies.forEach(withCounter((i, movie) -> {
57+
String ranking = (i + 1) + ": " + movie;
58+
rankings.add(ranking);
59+
}));
60+
61+
return rankings;
62+
}
63+
64+
public static <T> Consumer<T> withCounter(BiConsumer<Integer, T> consumer) {
65+
AtomicInteger counter = new AtomicInteger(0);
66+
return item -> consumer.accept(counter.getAndIncrement(), item);
67+
}
68+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.iterationcounter;
2+
3+
import org.junit.Test;
4+
5+
import static com.baeldung.iterationcounter.IterationCounter.*;
6+
import static org.assertj.core.api.Assertions.*;
7+
8+
public class IterationCounterUnitTest {
9+
@Test
10+
public void givenRankings_whenCalculateWithForLoop_thenRankingsCorrect() {
11+
assertThat(getRankingsWithForLoop(IMDB_TOP_MOVIES))
12+
.containsExactly("1: The Shawshank Redemption",
13+
"2: The Godfather", "3: The Godfather II", "4: The Dark Knight");
14+
}
15+
16+
@Test
17+
public void givenRankings_whenCalculateWithForEachLoop_thenRankingsCorrect() {
18+
assertThat(getRankingsWithForEachLoop(IMDB_TOP_MOVIES))
19+
.containsExactly("1: The Shawshank Redemption",
20+
"2: The Godfather", "3: The Godfather II", "4: The Dark Knight");
21+
}
22+
23+
@Test
24+
public void givenRankings_whenCalculateWithFunctionalForEach_thenRankingsCorrect() {
25+
assertThat(getRankingsWithFunctionalForEachLoop(IMDB_TOP_MOVIES))
26+
.containsExactly("1: The Shawshank Redemption",
27+
"2: The Godfather", "3: The Godfather II", "4: The Dark Knight");
28+
}
29+
30+
@Test
31+
public void givenRankings_whenCalculateWithStream_thenRankingsCorrect() {
32+
assertThat(getRankingsWithStream(IMDB_TOP_MOVIES.stream()))
33+
.containsExactly("1: The Shawshank Redemption",
34+
"2: The Godfather", "3: The Godfather II", "4: The Dark Knight");
35+
}
36+
}

0 commit comments

Comments
 (0)