Skip to content

Commit 07750c2

Browse files
authored
Merge pull request eugenp#3389 from k0l0ssus/master
Java-Vavr Stream Comparison Sample
2 parents 0859445 + 08f58c5 commit 07750c2

3 files changed

Lines changed: 119 additions & 0 deletions

File tree

‎java-vavr-stream/pom.xml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung.samples</groupId>
5+
<artifactId>java-vavr-stream</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>1.8</maven.compiler.source>
11+
<maven.compiler.target>1.8</maven.compiler.target>
12+
</properties>
13+
<dependencies>
14+
<dependency>
15+
<groupId>io.vavr</groupId>
16+
<artifactId>vavr</artifactId>
17+
<version>0.9.2</version>
18+
</dependency>
19+
</dependencies>
20+
</project>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.baeldung.samples.java.vavr;
2+
3+
import io.vavr.collection.Stream;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.stream.IntStream;
8+
9+
/**
10+
*
11+
* @author baeldung
12+
*/
13+
public class VavrSampler {
14+
15+
static int[] intArray = new int[]{1, 2, 4};
16+
static List<Integer> intList = new ArrayList<Integer>();
17+
static int[][] intOfInts = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
18+
19+
public static void main(String[] args) {
20+
vavrStreamElementAccess();
21+
System.out.println("====================================");
22+
vavrParallelStreamAccess();
23+
System.out.println("====================================");
24+
jdkFlatMapping();
25+
System.out.println("====================================");
26+
vavrStreamManipulation();
27+
System.out.println("====================================");
28+
vavrStreamDistinct();
29+
}
30+
31+
public static void vavrStreamElementAccess() {
32+
System.out.println("Vavr Element Access");
33+
System.out.println("====================================");
34+
Stream<Integer> vavredStream = Stream.ofAll(intArray);
35+
System.out.println("Vavr index access: " + vavredStream.get(2));
36+
System.out.println("Vavr head element access: " + vavredStream.get());
37+
38+
Stream<String> vavredStringStream = Stream.of("foo", "bar", "baz");
39+
System.out.println("Find foo " + vavredStringStream.indexOf("foo"));
40+
}
41+
42+
public static void vavrParallelStreamAccess() {
43+
try {
44+
System.out.println("Vavr Stream Concurrent Modification");
45+
System.out.println("====================================");
46+
Stream<Integer> vavrStream = Stream.ofAll(intList);
47+
intList.add(5);
48+
vavrStream.forEach(i -> System.out.println("in a Vavr Stream: " + i));
49+
} catch (Exception ex) {
50+
ex.printStackTrace();
51+
}
52+
53+
Stream<Integer> wrapped = Stream.ofAll(intArray);
54+
intArray[2] = 5;
55+
wrapped.forEach(i -> System.out.println("Vavr looped " + i));
56+
}
57+
58+
public static void jdkFlatMapping() {
59+
System.out.println("JDK FlatMap -> Uncomment line 68 to test");
60+
System.out.println("====================================");
61+
int[][] intOfInts = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
62+
63+
IntStream mapToInt = Arrays.stream(intOfInts)
64+
.map(intArr -> Arrays.stream(intArr))
65+
.flatMapToInt(val -> val.map(n -> {
66+
return n * n;
67+
}))
68+
.peek(n -> System.out.println("Peeking at " + n));
69+
//Uncomment to execute pipeline
70+
//mapToInt.forEach(n -> System.out.println("FlatMapped Result "+n));
71+
}
72+
73+
public static void vavrStreamManipulation() {
74+
System.out.println("Vavr Stream Manipulation");
75+
System.out.println("====================================");
76+
List<String> stringList = new ArrayList<>();
77+
stringList.add("foo");
78+
stringList.add("bar");
79+
stringList.add("baz");
80+
Stream<String> vavredStream = Stream.ofAll(stringList);
81+
vavredStream.forEach(item -> System.out.println("Vavr Stream item: " + item));
82+
Stream<String> vavredStream2 = vavredStream.insert(2, "buzz");
83+
vavredStream2.forEach(item -> System.out.println("Vavr Stream item after stream addition: " + item));
84+
stringList.forEach(item -> System.out.println("List item after stream addition: " + item));
85+
Stream<String> deletionStream = vavredStream.remove("bar");
86+
deletionStream.forEach(item -> System.out.println("Vavr Stream item after stream item deletion: " + item));
87+
88+
}
89+
90+
public static void vavrStreamDistinct() {
91+
Stream<String> vavredStream = Stream.of("foo", "bar", "baz", "buxx", "bar", "bar", "foo");
92+
Stream<String> distinctVavrStream = vavredStream.distinctBy((y, z) -> {
93+
return y.compareTo(z);
94+
});
95+
distinctVavrStream.forEach(item -> System.out.println("Vavr Stream item after distinct query " + item));
96+
97+
}
98+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<!-- <module>persistence-modules/java-cassandra</module> -->
8888
<module>vavr</module>
8989
<module>java-lite</module>
90+
<module>java-vavr-stream</module>
9091
<module>javax-servlets</module>
9192
<module>javaxval</module>
9293
<module>jaxb</module>

0 commit comments

Comments
 (0)