Skip to content

Commit

Permalink
BAEL-1078 Stream Indices - Update pom.xml (eugenp#2516)
Browse files Browse the repository at this point in the history
* Update pom.xml

* BAEL-1078 Stream indices
  • Loading branch information
pedja4 authored Aug 29, 2017
1 parent 8da9bee commit b3a291a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@
<artifactId>hirondelle-date4j</artifactId>
<version>${hirondelle-date4j.version}</version>
</dependency>
<dependency>
<groupId>com.codepoetics</groupId>
<artifactId>protonpack</artifactId>
<version>${protonpack.version}</version>
</dependency>
<dependency>
<groupId>one.util</groupId>
<artifactId>streamex</artifactId>
<version>${streamex.version}</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -420,6 +430,8 @@
<fscontext.version>4.6-b01</fscontext.version>
<joda-time.version>2.9.9</joda-time.version>
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
<protonpack.version>1.13</protonpack.version>
<streamex.version>0.6.5</streamex.version>

<!-- testing -->
<org.hamcrest.version>1.3</org.hamcrest.version>
Expand Down
37 changes: 37 additions & 0 deletions core-java/src/main/java/com/baeldung/stream/StreamIndices.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.baeldung.stream;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.codepoetics.protonpack.Indexed;
import com.codepoetics.protonpack.StreamUtils;

public class StreamIndices {

public static List<String> getEvenIndexedStrings(String[] names) {
List<String> evenIndexedNames = IntStream.range(0, names.length)
.filter(i -> i % 2 == 0).mapToObj(i -> names[i])
.collect(Collectors.toList());
return evenIndexedNames;
}

public static List<Indexed<String>> getEvenIndexedStrings(List<String> names) {
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
.filter(i -> i.getIndex() % 2 == 0).collect(Collectors.toList());
return list;
}

public static List<Indexed<String>> getOddIndexedStrings(List<String> names) {
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
.filter(i -> i.getIndex() % 2 == 1).collect(Collectors.toList());
return list;
}

public static List<String> getOddIndexedStrings(String[] names) {
List<String> oddIndexedNames = IntStream.range(0, names.length)
.filter(i -> i % 2 == 1).mapToObj(i -> names[i])
.collect(Collectors.toList());
return oddIndexedNames;
}
}
50 changes: 50 additions & 0 deletions core-java/src/test/java/com/baeldung/stream/StreamIndicesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.baeldung.stream;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;

import com.codepoetics.protonpack.Indexed;

public class StreamIndicesTest {

@Test
public void givenArray_whenGetIndexedStrings_thenReturnListOfEvenIndexedStrings() {
String[] names = { "Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim" };
List<String> expectedResult = Arrays.asList("Afrim", "Besim", "Durim");
List<String> actualResult = StreamIndices.getEvenIndexedStrings(names);

assertEquals(expectedResult, actualResult);
}

@Test
public void givenArray_whenGetIndexedStrings_thenReturnListOfOddStrings() {
String[] names = { "Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim" };
List<String> expectedResult = Arrays.asList("Bashkim", "Lulzim", "Shpetim");
List<String> actualResult = StreamIndices.getOddIndexedStrings(names);

assertEquals(expectedResult, actualResult);
}

@Test
public void givenList_whenGetIndexedStrings_thenReturnListOfEvenIndexedStrings() {
List<String> names = Arrays.asList("Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim");
List<Indexed<String>> expectedResult = Arrays.asList(Indexed.index(0, "Afrim"), Indexed.index(2, "Besim"), Indexed.index(4, "Durim"));
List<Indexed<String>> actualResult = StreamIndices.getEvenIndexedStrings(names);

assertEquals(expectedResult, actualResult);
}

@Test
public void givenList_whenGetIndexedStrings_thenReturnListOfOddIndexedStrings() {
List<String> names = Arrays.asList("Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim");
List<Indexed<String>> expectedResult = Arrays.asList(Indexed.index(1, "Bashkim"), Indexed.index(3, "Lulzim"), Indexed.index(5, "Shpetim"));
List<Indexed<String>> actualResult = StreamIndices.getOddIndexedStrings(names);

assertEquals(expectedResult, actualResult);
}

}

0 comments on commit b3a291a

Please sign in to comment.