Skip to content

Commit b3a291a

Browse files
authored
BAEL-1078 Stream Indices - Update pom.xml (eugenp#2516)
* Update pom.xml * BAEL-1078 Stream indices
1 parent 8da9bee commit b3a291a

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

core-java/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@
196196
<artifactId>hirondelle-date4j</artifactId>
197197
<version>${hirondelle-date4j.version}</version>
198198
</dependency>
199+
<dependency>
200+
<groupId>com.codepoetics</groupId>
201+
<artifactId>protonpack</artifactId>
202+
<version>${protonpack.version}</version>
203+
</dependency>
204+
<dependency>
205+
<groupId>one.util</groupId>
206+
<artifactId>streamex</artifactId>
207+
<version>${streamex.version}</version>
208+
</dependency>
199209
</dependencies>
200210

201211
<build>
@@ -420,6 +430,8 @@
420430
<fscontext.version>4.6-b01</fscontext.version>
421431
<joda-time.version>2.9.9</joda-time.version>
422432
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
433+
<protonpack.version>1.13</protonpack.version>
434+
<streamex.version>0.6.5</streamex.version>
423435

424436
<!-- testing -->
425437
<org.hamcrest.version>1.3</org.hamcrest.version>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.stream;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.IntStream;
6+
7+
import com.codepoetics.protonpack.Indexed;
8+
import com.codepoetics.protonpack.StreamUtils;
9+
10+
public class StreamIndices {
11+
12+
public static List<String> getEvenIndexedStrings(String[] names) {
13+
List<String> evenIndexedNames = IntStream.range(0, names.length)
14+
.filter(i -> i % 2 == 0).mapToObj(i -> names[i])
15+
.collect(Collectors.toList());
16+
return evenIndexedNames;
17+
}
18+
19+
public static List<Indexed<String>> getEvenIndexedStrings(List<String> names) {
20+
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
21+
.filter(i -> i.getIndex() % 2 == 0).collect(Collectors.toList());
22+
return list;
23+
}
24+
25+
public static List<Indexed<String>> getOddIndexedStrings(List<String> names) {
26+
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
27+
.filter(i -> i.getIndex() % 2 == 1).collect(Collectors.toList());
28+
return list;
29+
}
30+
31+
public static List<String> getOddIndexedStrings(String[] names) {
32+
List<String> oddIndexedNames = IntStream.range(0, names.length)
33+
.filter(i -> i % 2 == 1).mapToObj(i -> names[i])
34+
.collect(Collectors.toList());
35+
return oddIndexedNames;
36+
}
37+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.stream;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import org.junit.Test;
9+
10+
import com.codepoetics.protonpack.Indexed;
11+
12+
public class StreamIndicesTest {
13+
14+
@Test
15+
public void givenArray_whenGetIndexedStrings_thenReturnListOfEvenIndexedStrings() {
16+
String[] names = { "Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim" };
17+
List<String> expectedResult = Arrays.asList("Afrim", "Besim", "Durim");
18+
List<String> actualResult = StreamIndices.getEvenIndexedStrings(names);
19+
20+
assertEquals(expectedResult, actualResult);
21+
}
22+
23+
@Test
24+
public void givenArray_whenGetIndexedStrings_thenReturnListOfOddStrings() {
25+
String[] names = { "Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim" };
26+
List<String> expectedResult = Arrays.asList("Bashkim", "Lulzim", "Shpetim");
27+
List<String> actualResult = StreamIndices.getOddIndexedStrings(names);
28+
29+
assertEquals(expectedResult, actualResult);
30+
}
31+
32+
@Test
33+
public void givenList_whenGetIndexedStrings_thenReturnListOfEvenIndexedStrings() {
34+
List<String> names = Arrays.asList("Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim");
35+
List<Indexed<String>> expectedResult = Arrays.asList(Indexed.index(0, "Afrim"), Indexed.index(2, "Besim"), Indexed.index(4, "Durim"));
36+
List<Indexed<String>> actualResult = StreamIndices.getEvenIndexedStrings(names);
37+
38+
assertEquals(expectedResult, actualResult);
39+
}
40+
41+
@Test
42+
public void givenList_whenGetIndexedStrings_thenReturnListOfOddIndexedStrings() {
43+
List<String> names = Arrays.asList("Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim");
44+
List<Indexed<String>> expectedResult = Arrays.asList(Indexed.index(1, "Bashkim"), Indexed.index(3, "Lulzim"), Indexed.index(5, "Shpetim"));
45+
List<Indexed<String>> actualResult = StreamIndices.getOddIndexedStrings(names);
46+
47+
assertEquals(expectedResult, actualResult);
48+
}
49+
50+
}

0 commit comments

Comments
 (0)