BAEL-1078 Code formatting (#2539)

This commit is contained in:
Predrag Maric 2017-08-31 18:42:03 +02:00 committed by GitHub
parent 6d6b24590f
commit 12ffbe027d
3 changed files with 67 additions and 8 deletions

View File

@ -196,6 +196,11 @@
<artifactId>streamex</artifactId>
<version>${streamex.version}</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>${vavr.version}</version>
</dependency>
</dependencies>
<build>
@ -420,6 +425,7 @@
<fscontext.version>4.6-b01</fscontext.version>
<protonpack.version>1.13</protonpack.version>
<streamex.version>0.6.5</streamex.version>
<vavr.version>0.9.0</vavr.version>
<!-- testing -->
<org.hamcrest.version>1.3</org.hamcrest.version>

View File

@ -1,5 +1,6 @@
package com.baeldung.stream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@ -7,31 +8,65 @@ import java.util.stream.IntStream;
import com.codepoetics.protonpack.Indexed;
import com.codepoetics.protonpack.StreamUtils;
import io.vavr.Tuple2;
import io.vavr.collection.Stream;
import one.util.streamex.EntryStream;
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])
List<String> evenIndexedNames = IntStream
.range(0, names.length)
.filter(i -> i % 2 == 0)
.mapToObj(i -> names[i])
.collect(Collectors.toList());
return evenIndexedNames;
}
public List<String> getEvenIndexedStringsVersionTwo(List<String> names) {
List<String> evenIndexedNames = EntryStream
.of(names)
.filterKeyValue((index, name) -> index % 2 == 0)
.values()
.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());
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());
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])
List<String> oddIndexedNames = IntStream
.range(0, names.length)
.filter(i -> i % 2 == 1)
.mapToObj(i -> names[i])
.collect(Collectors.toList());
return oddIndexedNames;
}
public static List<String> getOddIndexedStringsVersionTwo(String[] names) {
List<Tuple2<String, Integer>> tuples = Stream
.of(names)
.zipWithIndex()
.filter(tuple -> tuple._2 % 2 == 1)
.toJavaList();
List<String> oddIndexedNames = new ArrayList<String>();
tuples.forEach(tuple -> {
oddIndexedNames.add(tuple._1);
});
return oddIndexedNames;
}
}

View File

@ -20,6 +20,15 @@ public class StreamIndicesTest {
assertEquals(expectedResult, actualResult);
}
@Test
public void givenArray_whenGetIndexedStrings_thenReturnListOfEvenIndexedStringsVersionTwo() {
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" };
@ -47,4 +56,13 @@ public class StreamIndicesTest {
assertEquals(expectedResult, actualResult);
}
@Test
public void givenArray_whenGetIndexedStrings_thenReturnListOfOddStringsVersionTwo() {
String[] names = { "Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim" };
List<String> expectedResult = Arrays.asList("Bashkim", "Lulzim", "Shpetim");
List<String> actualResult = StreamIndices.getOddIndexedStringsVersionTwo(names);
assertEquals(expectedResult, actualResult);
}
}