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
This commit is contained in:
parent
d635e9a245
commit
94d02ed1f6
|
@ -1,6 +1,5 @@
|
||||||
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;
|
||||||
|
@ -8,15 +7,13 @@ 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 io.vavr.collection.Stream;
|
||||||
import one.util.streamex.EntryStream;
|
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
|
List<String> evenIndexedNames = IntStream.range(0, names.length)
|
||||||
.range(0, names.length)
|
|
||||||
.filter(i -> i % 2 == 0)
|
.filter(i -> i % 2 == 0)
|
||||||
.mapToObj(i -> names[i])
|
.mapToObj(i -> names[i])
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
@ -24,8 +21,7 @@ public class StreamIndices {
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getEvenIndexedStringsVersionTwo(List<String> names) {
|
public List<String> getEvenIndexedStringsVersionTwo(List<String> names) {
|
||||||
List<String> evenIndexedNames = EntryStream
|
List<String> evenIndexedNames = EntryStream.of(names)
|
||||||
.of(names)
|
|
||||||
.filterKeyValue((index, name) -> index % 2 == 0)
|
.filterKeyValue((index, name) -> index % 2 == 0)
|
||||||
.values()
|
.values()
|
||||||
.toList();
|
.toList();
|
||||||
|
@ -33,24 +29,21 @@ public class StreamIndices {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Indexed<String>> getEvenIndexedStrings(List<String> names) {
|
public static List<Indexed<String>> getEvenIndexedStrings(List<String> names) {
|
||||||
List<Indexed<String>> list = StreamUtils
|
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
|
||||||
.zipWithIndex(names.stream())
|
|
||||||
.filter(i -> i.getIndex() % 2 == 0)
|
.filter(i -> i.getIndex() % 2 == 0)
|
||||||
.collect(Collectors.toList());
|
.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
|
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
|
||||||
.zipWithIndex(names.stream())
|
|
||||||
.filter(i -> i.getIndex() % 2 == 1)
|
.filter(i -> i.getIndex() % 2 == 1)
|
||||||
.collect(Collectors.toList());
|
.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
|
List<String> oddIndexedNames = IntStream.range(0, names.length)
|
||||||
.range(0, names.length)
|
|
||||||
.filter(i -> i % 2 == 1)
|
.filter(i -> i % 2 == 1)
|
||||||
.mapToObj(i -> names[i])
|
.mapToObj(i -> names[i])
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
@ -58,15 +51,12 @@ public class StreamIndices {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> getOddIndexedStringsVersionTwo(String[] names) {
|
public static List<String> getOddIndexedStringsVersionTwo(String[] names) {
|
||||||
List<Tuple2<String, Integer>> tuples = Stream
|
List<String> oddIndexedNames = Stream.of(names)
|
||||||
.of(names)
|
|
||||||
.zipWithIndex()
|
.zipWithIndex()
|
||||||
.filter(tuple -> tuple._2 % 2 == 1)
|
.filter(tuple -> tuple._2 % 2 == 1)
|
||||||
|
.map(tuple -> tuple._1)
|
||||||
.toJavaList();
|
.toJavaList();
|
||||||
List<String> oddIndexedNames = new ArrayList<String>();
|
|
||||||
tuples.forEach(tuple -> {
|
|
||||||
oddIndexedNames.add(tuple._1);
|
|
||||||
});
|
|
||||||
return oddIndexedNames;
|
return oddIndexedNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue