Skip to content

Commit 94d02ed

Browse files
fatosmorinaEugen
authored andcommitted
BAEL-1078 How to iterate over a stream with indices (eugenp#2544)
* Add the example with EntryStream * Add the example with EntryStream * Add vavr * Implement the method getOddIndexedStringsVersionTwo * Implement the method givenArray_whenGetIndexedStrings_thenReturnListOfOddStringsVersionTwo * BAEL-1078 Code formatting * Extract tuples using map * Fix merge conflicts
1 parent d635e9a commit 94d02ed

1 file changed

Lines changed: 24 additions & 34 deletions

File tree

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,62 @@
11
package com.baeldung.stream;
22

3-
import java.util.ArrayList;
43
import java.util.List;
54
import java.util.stream.Collectors;
65
import java.util.stream.IntStream;
76

87
import com.codepoetics.protonpack.Indexed;
98
import com.codepoetics.protonpack.StreamUtils;
109

11-
import io.vavr.Tuple2;
1210
import io.vavr.collection.Stream;
1311
import one.util.streamex.EntryStream;
1412

1513
public class StreamIndices {
1614

1715
public static List<String> getEvenIndexedStrings(String[] names) {
18-
List<String> evenIndexedNames = IntStream
19-
.range(0, names.length)
20-
.filter(i -> i % 2 == 0)
21-
.mapToObj(i -> names[i])
22-
.collect(Collectors.toList());
16+
List<String> evenIndexedNames = IntStream.range(0, names.length)
17+
.filter(i -> i % 2 == 0)
18+
.mapToObj(i -> names[i])
19+
.collect(Collectors.toList());
2320
return evenIndexedNames;
2421
}
2522

2623
public List<String> getEvenIndexedStringsVersionTwo(List<String> names) {
27-
List<String> evenIndexedNames = EntryStream
28-
.of(names)
29-
.filterKeyValue((index, name) -> index % 2 == 0)
30-
.values()
31-
.toList();
24+
List<String> evenIndexedNames = EntryStream.of(names)
25+
.filterKeyValue((index, name) -> index % 2 == 0)
26+
.values()
27+
.toList();
3228
return evenIndexedNames;
3329
}
3430

3531
public static List<Indexed<String>> getEvenIndexedStrings(List<String> names) {
36-
List<Indexed<String>> list = StreamUtils
37-
.zipWithIndex(names.stream())
38-
.filter(i -> i.getIndex() % 2 == 0)
39-
.collect(Collectors.toList());
32+
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
33+
.filter(i -> i.getIndex() % 2 == 0)
34+
.collect(Collectors.toList());
4035
return list;
4136
}
4237

4338
public static List<Indexed<String>> getOddIndexedStrings(List<String> names) {
44-
List<Indexed<String>> list = StreamUtils
45-
.zipWithIndex(names.stream())
46-
.filter(i -> i.getIndex() % 2 == 1)
47-
.collect(Collectors.toList());
39+
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
40+
.filter(i -> i.getIndex() % 2 == 1)
41+
.collect(Collectors.toList());
4842
return list;
4943
}
5044

5145
public static List<String> getOddIndexedStrings(String[] names) {
52-
List<String> oddIndexedNames = IntStream
53-
.range(0, names.length)
54-
.filter(i -> i % 2 == 1)
55-
.mapToObj(i -> names[i])
56-
.collect(Collectors.toList());
46+
List<String> oddIndexedNames = IntStream.range(0, names.length)
47+
.filter(i -> i % 2 == 1)
48+
.mapToObj(i -> names[i])
49+
.collect(Collectors.toList());
5750
return oddIndexedNames;
5851
}
5952

6053
public static List<String> getOddIndexedStringsVersionTwo(String[] names) {
61-
List<Tuple2<String, Integer>> tuples = Stream
62-
.of(names)
63-
.zipWithIndex()
64-
.filter(tuple -> tuple._2 % 2 == 1)
65-
.toJavaList();
66-
List<String> oddIndexedNames = new ArrayList<String>();
67-
tuples.forEach(tuple -> {
68-
oddIndexedNames.add(tuple._1);
69-
});
54+
List<String> oddIndexedNames = Stream.of(names)
55+
.zipWithIndex()
56+
.filter(tuple -> tuple._2 % 2 == 1)
57+
.map(tuple -> tuple._1)
58+
.toJavaList();
7059
return oddIndexedNames;
7160
}
61+
7262
}

0 commit comments

Comments
 (0)