From 9fe90d46633e32d7ba34d8b4e6240e49449e2474 Mon Sep 17 00:00:00 2001 From: vaibhav007jain <72961247+vaibhav007jain@users.noreply.github.com> Date: Sun, 21 May 2023 08:15:43 +0530 Subject: [PATCH] Bael-6331: Adding changes for creating and checking empty streams (#14047) * BAEL-6331: Create EmptyStreams.java * BAEL-6331: Create EmptyStreamsUnitTest.java --- .../streams/emptystreams/EmptyStreams.java | 49 +++++++++++++++++++ .../emptystreams/EmptyStreamsUnitTest.java | 44 +++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 core-java-modules/core-java-streams-5/src/main/java/com/baeldung/streams/emptystreams/EmptyStreams.java create mode 100644 core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/emptystreams/EmptyStreamsUnitTest.java diff --git a/core-java-modules/core-java-streams-5/src/main/java/com/baeldung/streams/emptystreams/EmptyStreams.java b/core-java-modules/core-java-streams-5/src/main/java/com/baeldung/streams/emptystreams/EmptyStreams.java new file mode 100644 index 0000000000..e561cfc8c1 --- /dev/null +++ b/core-java-modules/core-java-streams-5/src/main/java/com/baeldung/streams/emptystreams/EmptyStreams.java @@ -0,0 +1,49 @@ +package com.baeldung.streams.emptystreams; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Optional; +import java.util.function.Supplier; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +public class EmptyStreams { + + public static void main(String[] args) { + + createEmptyStreams(); + checkForEmptyStreamUsingSupplier(); + } + + private static void createEmptyStreams() { + + // Using Stream.empty() + Stream emptyStream = Stream.empty(); + System.out.println(emptyStream.findAny().isEmpty()); + + // Using Stream.of() + emptyStream = Stream.of(); + System.out.println(emptyStream.findAny().isEmpty()); + + // Empty Stream of primitive type + IntStream emptyIntStream = IntStream.of(new int[] {}); + System.out.println(emptyIntStream.findAny().isEmpty()); + + // Using Arrays.stream() + emptyIntStream = Arrays.stream(new int[] {}); + System.out.println(emptyIntStream.findAny().isEmpty()); + + // Using list.stream() + Stream collectionStream = new ArrayList().stream(); + System.out.println(collectionStream.findAny().isEmpty()); + } + + private static void checkForEmptyStreamUsingSupplier() { + Supplier> streamSupplier = () -> Stream.of(1, 2, 3, 4, 5).filter(number -> number > 5); + + Optional result1 = streamSupplier.get().findAny(); + System.out.println(result1.isEmpty()); + Optional result2 = streamSupplier.get().findFirst(); + System.out.println(result2.isEmpty()); + } +} diff --git a/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/emptystreams/EmptyStreamsUnitTest.java b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/emptystreams/EmptyStreamsUnitTest.java new file mode 100644 index 0000000000..a05eca1fcb --- /dev/null +++ b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/emptystreams/EmptyStreamsUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.streams.emptystreams; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Optional; +import java.util.function.Supplier; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import org.junit.Test; + +public class EmptyStreamsUnitTest { + + @Test + public void givenEmptyStreams_findAnyReturnsAnEmptyOptional() { + + Stream emptyStream = Stream.empty(); + assertTrue(emptyStream.findAny().isEmpty()); + + emptyStream = Stream.of(); + assertTrue(emptyStream.findAny().isEmpty()); + + IntStream emptyIntStream = IntStream.of(new int[] {}); + assertTrue(emptyIntStream.findAny().isEmpty()); + + emptyIntStream = Arrays.stream(new int[] {}); + assertTrue(emptyIntStream.findAny().isEmpty()); + + Stream collectionStream = new ArrayList().stream(); + assertTrue(collectionStream.findAny().isEmpty()); + } + + @Test + public void givenAStreamToSupplier_NewInstanceOfTheStreamIsReturnedForEveryGetCall() { + Supplier> streamSupplier = () -> Stream.of(1, 2, 3, 4, 5).filter(number -> number > 5); + + Optional result1 = streamSupplier.get().findAny(); + assertTrue(result1.isEmpty()); + Optional result2 = streamSupplier.get().findFirst(); + assertTrue(result2.isEmpty()); + } +}