Bael-6331: Adding changes for creating and checking empty streams (#14047)

* BAEL-6331: Create EmptyStreams.java

* BAEL-6331: Create EmptyStreamsUnitTest.java
This commit is contained in:
vaibhav007jain 2023-05-21 08:15:43 +05:30 committed by GitHub
parent 7a8afbb58f
commit 9fe90d4663
2 changed files with 93 additions and 0 deletions

View File

@ -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<String> 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<Integer> collectionStream = new ArrayList<Integer>().stream();
System.out.println(collectionStream.findAny().isEmpty());
}
private static void checkForEmptyStreamUsingSupplier() {
Supplier<Stream<Integer>> streamSupplier = () -> Stream.of(1, 2, 3, 4, 5).filter(number -> number > 5);
Optional<Integer> result1 = streamSupplier.get().findAny();
System.out.println(result1.isEmpty());
Optional<Integer> result2 = streamSupplier.get().findFirst();
System.out.println(result2.isEmpty());
}
}

View File

@ -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<String> 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<Integer> collectionStream = new ArrayList<Integer>().stream();
assertTrue(collectionStream.findAny().isEmpty());
}
@Test
public void givenAStreamToSupplier_NewInstanceOfTheStreamIsReturnedForEveryGetCall() {
Supplier<Stream<Integer>> streamSupplier = () -> Stream.of(1, 2, 3, 4, 5).filter(number -> number > 5);
Optional<Integer> result1 = streamSupplier.get().findAny();
assertTrue(result1.isEmpty());
Optional<Integer> result2 = streamSupplier.get().findFirst();
assertTrue(result2.isEmpty());
}
}