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