11package com .baeldung .stream ;
22
3- import java .util .ArrayList ;
43import java .util .List ;
54import java .util .stream .Collectors ;
65import java .util .stream .IntStream ;
76
87import com .codepoetics .protonpack .Indexed ;
98import com .codepoetics .protonpack .StreamUtils ;
109
11- import io .vavr .Tuple2 ;
1210import io .vavr .collection .Stream ;
1311import one .util .streamex .EntryStream ;
1412
1513public 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