diff --git a/java-vavr-stream/pom.xml b/java-vavr-stream/pom.xml new file mode 100644 index 0000000000..ca3807cd15 --- /dev/null +++ b/java-vavr-stream/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + com.baeldung.samples + java-vavr-stream + 1.0 + jar + + UTF-8 + 1.8 + 1.8 + + + + io.vavr + vavr + 0.9.2 + + + \ No newline at end of file diff --git a/java-vavr-stream/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java b/java-vavr-stream/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java new file mode 100644 index 0000000000..d539342f69 --- /dev/null +++ b/java-vavr-stream/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java @@ -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 intList = new ArrayList(); + 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 vavredStream = Stream.ofAll(intArray); + System.out.println("Vavr index access: " + vavredStream.get(2)); + System.out.println("Vavr head element access: " + vavredStream.get()); + + Stream 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 vavrStream = Stream.ofAll(intList); + intList.add(5); + vavrStream.forEach(i -> System.out.println("in a Vavr Stream: " + i)); + } catch (Exception ex) { + ex.printStackTrace(); + } + + Stream 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 stringList = new ArrayList<>(); + stringList.add("foo"); + stringList.add("bar"); + stringList.add("baz"); + Stream vavredStream = Stream.ofAll(stringList); + vavredStream.forEach(item -> System.out.println("Vavr Stream item: " + item)); + Stream 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 deletionStream = vavredStream.remove("bar"); + deletionStream.forEach(item -> System.out.println("Vavr Stream item after stream item deletion: " + item)); + + } + + public static void vavrStreamDistinct() { + Stream vavredStream = Stream.of("foo", "bar", "baz", "buxx", "bar", "bar", "foo"); + Stream distinctVavrStream = vavredStream.distinctBy((y, z) -> { + return y.compareTo(z); + }); + distinctVavrStream.forEach(item -> System.out.println("Vavr Stream item after distinct query " + item)); + + } +}