This commit is contained in:
Seun Matt 2017-09-02 13:51:42 +01:00
commit 56e3687cad
6 changed files with 29 additions and 43 deletions

View File

@ -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<String> getEvenIndexedStrings(String[] names) {
List<String> evenIndexedNames = IntStream
.range(0, names.length)
.filter(i -> i % 2 == 0)
.mapToObj(i -> names[i])
.collect(Collectors.toList());
List<String> evenIndexedNames = IntStream.range(0, names.length)
.filter(i -> i % 2 == 0)
.mapToObj(i -> names[i])
.collect(Collectors.toList());
return evenIndexedNames;
}
public List<String> getEvenIndexedStringsVersionTwo(List<String> names) {
List<String> evenIndexedNames = EntryStream
.of(names)
.filterKeyValue((index, name) -> index % 2 == 0)
.values()
.toList();
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) {
List<Indexed<String>> list = StreamUtils
.zipWithIndex(names.stream())
.filter(i -> i.getIndex() % 2 == 0)
.collect(Collectors.toList());
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
.filter(i -> i.getIndex() % 2 == 0)
.collect(Collectors.toList());
return list;
}
public static List<Indexed<String>> getOddIndexedStrings(List<String> names) {
List<Indexed<String>> list = StreamUtils
.zipWithIndex(names.stream())
.filter(i -> i.getIndex() % 2 == 1)
.collect(Collectors.toList());
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
.filter(i -> i.getIndex() % 2 == 1)
.collect(Collectors.toList());
return list;
}
public static List<String> getOddIndexedStrings(String[] names) {
List<String> oddIndexedNames = IntStream
.range(0, names.length)
.filter(i -> i % 2 == 1)
.mapToObj(i -> names[i])
.collect(Collectors.toList());
List<String> oddIndexedNames = IntStream.range(0, names.length)
.filter(i -> i % 2 == 1)
.mapToObj(i -> names[i])
.collect(Collectors.toList());
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);
});
List<String> oddIndexedNames = Stream.of(names)
.zipWithIndex()
.filter(tuple -> tuple._2 % 2 == 1)
.map(tuple -> tuple._1)
.toJavaList();
return oddIndexedNames;
}
}

View File

@ -22,7 +22,7 @@
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-client</artifactId>
<version>3.7.2</version>
<version>${hazelcast.version}</version>
</dependency>
</dependencies>
@ -39,7 +39,7 @@
<properties>
<!-- hazelcast -->
<hazelcast.version>3.7.4</hazelcast.version>
<hazelcast.version>3.8.4</hazelcast.version>
</properties>
</project>

View File

@ -31,7 +31,7 @@
</dependencies>
<properties>
<hbase.version>1.3.0</hbase.version>
<hbase.version>1.3.1</hbase.version>
</properties>

View File

@ -3,7 +3,6 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>immutables</artifactId>
<version>1.0.0-SNAPSHOT</version>
@ -34,7 +33,7 @@
</dependencies>
<properties>
<immutables.version>2.3.10</immutables.version>
<immutables.version>2.5.6</immutables.version>
<assertj.version>3.6.1</assertj.version>
<mutabilitydetector.version>0.9.6</mutabilitydetector.version>
</properties>

View File

@ -1,7 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>json-path</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>json-path</name>

View File

@ -6,8 +6,6 @@
<modelVersion>4.0.0</modelVersion>
<name>lombok</name>
<groupId>com.baeldung</groupId>
<artifactId>lombok</artifactId>
<version>0.1-SNAPSHOT</version>
@ -81,7 +79,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- lombok: https://projectlombok.org/changelog.html -->
<lombok.version>1.16.12</lombok.version>
<lombok.version>1.16.18</lombok.version>
<!-- various -->
<hibernate-jpa-2.1-api.version>1.0.0.Final</hibernate-jpa-2.1-api.version>