[Java 11502] (#12625)
* [JAVA-11502] Added vavr-modules (parent) * [JAVA-11502] Added vavr(submodule) to vavr-modules(parent) * [JAVA-11502] Added vavr-2(submodule) to vavr-modules(parent) * [JAVA-11502] Added java-vavr-stream(submodule) to vavr-modules(parent) * [JAVA-11502] deleted modules that were moved + cleanup Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com> Co-authored-by: Dhawal Kapil <dhawalkapil@gmail.com>
This commit is contained in:
8
vavr-modules/java-vavr-stream/README.md
Normal file
8
vavr-modules/java-vavr-stream/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
## Vavr Streams
|
||||
|
||||
This module contains articles about streams in Vavr.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Java Streams vs Vavr Streams](https://www.baeldung.com/vavr-java-streams)
|
||||
|
||||
30
vavr-modules/java-vavr-stream/pom.xml
Normal file
30
vavr-modules/java-vavr-stream/pom.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.samples</groupId>
|
||||
<artifactId>java-vavr-stream</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>java-vavr-stream</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>vavr-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.vavr</groupId>
|
||||
<artifactId>vavr</artifactId>
|
||||
<version>${io.vavr.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<io.vavr.version>0.9.2</io.vavr.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.baeldung.samples.java.vavr;
|
||||
|
||||
import io.vavr.collection.Stream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author baeldung
|
||||
*/
|
||||
public class VavrSampler {
|
||||
|
||||
static int[] intArray = new int[]{1, 2, 4};
|
||||
static List<Integer> intList = new ArrayList<Integer>();
|
||||
static int[][] intOfInts = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||
|
||||
public static void main(String[] args) {
|
||||
vavrStreamElementAccess();
|
||||
System.out.println("====================================");
|
||||
vavrParallelStreamAccess();
|
||||
System.out.println("====================================");
|
||||
jdkFlatMapping();
|
||||
System.out.println("====================================");
|
||||
vavrStreamManipulation();
|
||||
System.out.println("====================================");
|
||||
vavrStreamDistinct();
|
||||
}
|
||||
|
||||
public static void vavrStreamElementAccess() {
|
||||
System.out.println("Vavr Element Access");
|
||||
System.out.println("====================================");
|
||||
Stream<Integer> vavredStream = Stream.ofAll(intArray);
|
||||
System.out.println("Vavr index access: " + vavredStream.get(2));
|
||||
System.out.println("Vavr head element access: " + vavredStream.get());
|
||||
|
||||
Stream<String> vavredStringStream = Stream.of("foo", "bar", "baz");
|
||||
System.out.println("Find foo " + vavredStringStream.indexOf("foo"));
|
||||
}
|
||||
|
||||
public static void vavrParallelStreamAccess() {
|
||||
try {
|
||||
System.out.println("Vavr Stream Concurrent Modification");
|
||||
System.out.println("====================================");
|
||||
Stream<Integer> vavrStream = Stream.ofAll(intList);
|
||||
intList.add(5);
|
||||
vavrStream.forEach(i -> System.out.println("in a Vavr Stream: " + i));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
Stream<Integer> wrapped = Stream.ofAll(intArray);
|
||||
intArray[2] = 5;
|
||||
wrapped.forEach(i -> System.out.println("Vavr looped " + i));
|
||||
}
|
||||
|
||||
public static void jdkFlatMapping() {
|
||||
System.out.println("JDK FlatMap -> Uncomment line 68 to test");
|
||||
System.out.println("====================================");
|
||||
int[][] intOfInts = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||
|
||||
IntStream mapToInt = Arrays.stream(intOfInts)
|
||||
.map(intArr -> Arrays.stream(intArr))
|
||||
.flatMapToInt(val -> val.map(n -> {
|
||||
return n * n;
|
||||
}))
|
||||
.peek(n -> System.out.println("Peeking at " + n));
|
||||
//Uncomment to execute pipeline
|
||||
//mapToInt.forEach(n -> System.out.println("FlatMapped Result "+n));
|
||||
}
|
||||
|
||||
public static void vavrStreamManipulation() {
|
||||
System.out.println("Vavr Stream Manipulation");
|
||||
System.out.println("====================================");
|
||||
List<String> stringList = new ArrayList<>();
|
||||
stringList.add("foo");
|
||||
stringList.add("bar");
|
||||
stringList.add("baz");
|
||||
Stream<String> vavredStream = Stream.ofAll(stringList);
|
||||
vavredStream.forEach(item -> System.out.println("Vavr Stream item: " + item));
|
||||
Stream<String> vavredStream2 = vavredStream.insert(2, "buzz");
|
||||
vavredStream2.forEach(item -> System.out.println("Vavr Stream item after stream addition: " + item));
|
||||
stringList.forEach(item -> System.out.println("List item after stream addition: " + item));
|
||||
Stream<String> deletionStream = vavredStream.remove("bar");
|
||||
deletionStream.forEach(item -> System.out.println("Vavr Stream item after stream item deletion: " + item));
|
||||
|
||||
}
|
||||
|
||||
public static void vavrStreamDistinct() {
|
||||
Stream<String> vavredStream = Stream.of("foo", "bar", "baz", "buxx", "bar", "bar", "foo");
|
||||
Stream<String> distinctVavrStream = vavredStream.distinctBy((y, z) -> {
|
||||
return y.compareTo(z);
|
||||
});
|
||||
distinctVavrStream.forEach(item -> System.out.println("Vavr Stream item after distinct query " + item));
|
||||
|
||||
}
|
||||
}
|
||||
13
vavr-modules/java-vavr-stream/src/main/resources/logback.xml
Normal file
13
vavr-modules/java-vavr-stream/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user