From 12ffbe027daab383c640efd86598e13a23e08143 Mon Sep 17 00:00:00 2001 From: Predrag Maric Date: Thu, 31 Aug 2017 18:42:03 +0200 Subject: [PATCH] BAEL-1078 Code formatting (#2539) --- core-java/pom.xml | 6 +++ .../com/baeldung/stream/StreamIndices.java | 51 ++++++++++++++++--- .../baeldung/stream/StreamIndicesTest.java | 18 +++++++ 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/core-java/pom.xml b/core-java/pom.xml index 633656857e..3805506181 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -196,6 +196,11 @@ streamex ${streamex.version} + + io.vavr + vavr + ${vavr.version} + @@ -420,6 +425,7 @@ 4.6-b01 1.13 0.6.5 + 0.9.0 1.3 diff --git a/core-java/src/main/java/com/baeldung/stream/StreamIndices.java b/core-java/src/main/java/com/baeldung/stream/StreamIndices.java index b101bc2740..aa4da58818 100644 --- a/core-java/src/main/java/com/baeldung/stream/StreamIndices.java +++ b/core-java/src/main/java/com/baeldung/stream/StreamIndices.java @@ -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 getEvenIndexedStrings(String[] names) { - List evenIndexedNames = IntStream.range(0, names.length) - .filter(i -> i % 2 == 0).mapToObj(i -> names[i]) + List evenIndexedNames = IntStream + .range(0, names.length) + .filter(i -> i % 2 == 0) + .mapToObj(i -> names[i]) .collect(Collectors.toList()); return evenIndexedNames; } + public List getEvenIndexedStringsVersionTwo(List names) { + List evenIndexedNames = EntryStream + .of(names) + .filterKeyValue((index, name) -> index % 2 == 0) + .values() + .toList(); + return evenIndexedNames; + } + public static List> getEvenIndexedStrings(List names) { - List> list = StreamUtils.zipWithIndex(names.stream()) - .filter(i -> i.getIndex() % 2 == 0).collect(Collectors.toList()); + List> list = StreamUtils + .zipWithIndex(names.stream()) + .filter(i -> i.getIndex() % 2 == 0) + .collect(Collectors.toList()); return list; } public static List> getOddIndexedStrings(List names) { - List> list = StreamUtils.zipWithIndex(names.stream()) - .filter(i -> i.getIndex() % 2 == 1).collect(Collectors.toList()); + List> list = StreamUtils + .zipWithIndex(names.stream()) + .filter(i -> i.getIndex() % 2 == 1) + .collect(Collectors.toList()); return list; } public static List getOddIndexedStrings(String[] names) { - List oddIndexedNames = IntStream.range(0, names.length) - .filter(i -> i % 2 == 1).mapToObj(i -> names[i]) + List oddIndexedNames = IntStream + .range(0, names.length) + .filter(i -> i % 2 == 1) + .mapToObj(i -> names[i]) .collect(Collectors.toList()); return oddIndexedNames; } + + public static List getOddIndexedStringsVersionTwo(String[] names) { + List> tuples = Stream + .of(names) + .zipWithIndex() + .filter(tuple -> tuple._2 % 2 == 1) + .toJavaList(); + List oddIndexedNames = new ArrayList(); + tuples.forEach(tuple -> { + oddIndexedNames.add(tuple._1); + }); + return oddIndexedNames; + } } \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/stream/StreamIndicesTest.java b/core-java/src/test/java/com/baeldung/stream/StreamIndicesTest.java index ca60544788..d8513417ea 100644 --- a/core-java/src/test/java/com/baeldung/stream/StreamIndicesTest.java +++ b/core-java/src/test/java/com/baeldung/stream/StreamIndicesTest.java @@ -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 expectedResult = Arrays.asList("Afrim", "Besim", "Durim"); + List 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 expectedResult = Arrays.asList("Bashkim", "Lulzim", "Shpetim"); + List actualResult = StreamIndices.getOddIndexedStringsVersionTwo(names); + + assertEquals(expectedResult, actualResult); + } + } \ No newline at end of file