BAEL-1078 Code formatting (#2539)
This commit is contained in:
parent
6d6b24590f
commit
12ffbe027d
@ -196,6 +196,11 @@
|
|||||||
<artifactId>streamex</artifactId>
|
<artifactId>streamex</artifactId>
|
||||||
<version>${streamex.version}</version>
|
<version>${streamex.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.vavr</groupId>
|
||||||
|
<artifactId>vavr</artifactId>
|
||||||
|
<version>${vavr.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -420,6 +425,7 @@
|
|||||||
<fscontext.version>4.6-b01</fscontext.version>
|
<fscontext.version>4.6-b01</fscontext.version>
|
||||||
<protonpack.version>1.13</protonpack.version>
|
<protonpack.version>1.13</protonpack.version>
|
||||||
<streamex.version>0.6.5</streamex.version>
|
<streamex.version>0.6.5</streamex.version>
|
||||||
|
<vavr.version>0.9.0</vavr.version>
|
||||||
|
|
||||||
<!-- testing -->
|
<!-- testing -->
|
||||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.baeldung.stream;
|
package com.baeldung.stream;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
@ -7,31 +8,65 @@ import java.util.stream.IntStream;
|
|||||||
import com.codepoetics.protonpack.Indexed;
|
import com.codepoetics.protonpack.Indexed;
|
||||||
import com.codepoetics.protonpack.StreamUtils;
|
import com.codepoetics.protonpack.StreamUtils;
|
||||||
|
|
||||||
|
import io.vavr.Tuple2;
|
||||||
|
import io.vavr.collection.Stream;
|
||||||
|
import one.util.streamex.EntryStream;
|
||||||
|
|
||||||
public class StreamIndices {
|
public class StreamIndices {
|
||||||
|
|
||||||
public static List<String> getEvenIndexedStrings(String[] names) {
|
public static List<String> getEvenIndexedStrings(String[] names) {
|
||||||
List<String> evenIndexedNames = IntStream.range(0, names.length)
|
List<String> evenIndexedNames = IntStream
|
||||||
.filter(i -> i % 2 == 0).mapToObj(i -> names[i])
|
.range(0, names.length)
|
||||||
|
.filter(i -> i % 2 == 0)
|
||||||
|
.mapToObj(i -> names[i])
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
return evenIndexedNames;
|
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) {
|
public static List<Indexed<String>> getEvenIndexedStrings(List<String> names) {
|
||||||
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
|
List<Indexed<String>> list = StreamUtils
|
||||||
.filter(i -> i.getIndex() % 2 == 0).collect(Collectors.toList());
|
.zipWithIndex(names.stream())
|
||||||
|
.filter(i -> i.getIndex() % 2 == 0)
|
||||||
|
.collect(Collectors.toList());
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Indexed<String>> getOddIndexedStrings(List<String> names) {
|
public static List<Indexed<String>> getOddIndexedStrings(List<String> names) {
|
||||||
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
|
List<Indexed<String>> list = StreamUtils
|
||||||
.filter(i -> i.getIndex() % 2 == 1).collect(Collectors.toList());
|
.zipWithIndex(names.stream())
|
||||||
|
.filter(i -> i.getIndex() % 2 == 1)
|
||||||
|
.collect(Collectors.toList());
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> getOddIndexedStrings(String[] names) {
|
public static List<String> getOddIndexedStrings(String[] names) {
|
||||||
List<String> oddIndexedNames = IntStream.range(0, names.length)
|
List<String> oddIndexedNames = IntStream
|
||||||
.filter(i -> i % 2 == 1).mapToObj(i -> names[i])
|
.range(0, names.length)
|
||||||
|
.filter(i -> i % 2 == 1)
|
||||||
|
.mapToObj(i -> names[i])
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
return oddIndexedNames;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
@ -20,6 +20,15 @@ public class StreamIndicesTest {
|
|||||||
assertEquals(expectedResult, actualResult);
|
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
|
@Test
|
||||||
public void givenArray_whenGetIndexedStrings_thenReturnListOfOddStrings() {
|
public void givenArray_whenGetIndexedStrings_thenReturnListOfOddStrings() {
|
||||||
String[] names = { "Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim" };
|
String[] names = { "Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim" };
|
||||||
@ -47,4 +56,13 @@ public class StreamIndicesTest {
|
|||||||
assertEquals(expectedResult, actualResult);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user