From 94d02ed1f6c960db7bae229f3b3f0cbdb9ab4c9b Mon Sep 17 00:00:00 2001 From: Fatos Morina Date: Sat, 2 Sep 2017 10:42:24 +0200 Subject: [PATCH] BAEL-1078 How to iterate over a stream with indices (#2544) * Add the example with EntryStream * Add the example with EntryStream * Add vavr * Implement the method getOddIndexedStringsVersionTwo * Implement the method givenArray_whenGetIndexedStrings_thenReturnListOfOddStringsVersionTwo * BAEL-1078 Code formatting * Extract tuples using map * Fix merge conflicts --- .../com/baeldung/stream/StreamIndices.java | 58 ++++++++----------- 1 file changed, 24 insertions(+), 34 deletions(-) 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 aa4da58818..34768a725a 100644 --- a/core-java/src/main/java/com/baeldung/stream/StreamIndices.java +++ b/core-java/src/main/java/com/baeldung/stream/StreamIndices.java @@ -1,6 +1,5 @@ package com.baeldung.stream; -import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -8,65 +7,56 @@ 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]) - .collect(Collectors.toList()); + 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(); + 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]) - .collect(Collectors.toList()); + 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); - }); + List oddIndexedNames = Stream.of(names) + .zipWithIndex() + .filter(tuple -> tuple._2 % 2 == 1) + .map(tuple -> tuple._1) + .toJavaList(); return oddIndexedNames; } + } \ No newline at end of file