BAEL-1078 Stream Indices - Update pom.xml (#2516)

* Update pom.xml

* BAEL-1078 Stream indices
This commit is contained in:
Predrag Maric
2017-08-29 07:36:55 +02:00
committed by GitHub
parent 8da9bee6aa
commit b3a291a580
3 changed files with 99 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -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);
}
}