From 3df5c8210dad905c3d7d53bfe3a16f704c0385ae Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 6 Nov 2016 18:17:07 +0200 Subject: [PATCH] code snippets for the `Java 9 Stream API improvements` article (#809) * code snippets for the `Java 9 Stream API improvements` article * code snippets for the `Java 9 Stream API improvements` article [2 attempt] * removed the first attempt --- .../language/stream/StreamFeaturesTest.java | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 core-java-9/src/test/java/com/baeldung/java9/language/stream/StreamFeaturesTest.java diff --git a/core-java-9/src/test/java/com/baeldung/java9/language/stream/StreamFeaturesTest.java b/core-java-9/src/test/java/com/baeldung/java9/language/stream/StreamFeaturesTest.java new file mode 100644 index 0000000000..a260e84164 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/language/stream/StreamFeaturesTest.java @@ -0,0 +1,119 @@ +package com.baeldung.java9.language.stream; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static java.lang.Integer.*; +import static org.junit.Assert.assertEquals; + +public class StreamFeaturesTest { + + public static class TakeAndDropWhileTest { + + public Stream getStreamAfterTakeWhileOperation() { + return Stream + .iterate("", s -> s + "s") + .takeWhile(s -> s.length() < 10); + } + + public Stream getStreamAfterDropWhileOperation() { + return Stream + .iterate("", s -> s + "s") + .takeWhile(s -> s.length() < 10) + .dropWhile(s -> !s.contains("sssss")); + } + + @Test + public void testTakeWhileOperation() { + List list = getStreamAfterTakeWhileOperation().collect(Collectors.toList()); + + assertEquals(10, list.size()); + + assertEquals("", list.get(0)); + assertEquals("ss", list.get(2)); + assertEquals("sssssssss", list.get(list.size() - 1)); + } + + @Test + public void testDropWhileOperation() { + List list = getStreamAfterDropWhileOperation().collect(Collectors.toList()); + + assertEquals(5, list.size()); + + assertEquals("sssss", list.get(0)); + assertEquals("sssssss", list.get(2)); + assertEquals("sssssssss", list.get(list.size() - 1)); + } + + } + + public static class IterateTest { + + private Stream getStream() { + return Stream.iterate(0, i -> i < 10, i -> i + 1); + } + + @Test + public void testIterateOperation() { + List list = getStream().collect(Collectors.toList()); + + assertEquals(10, list.size()); + + assertEquals(valueOf(0), list.get(0)); + assertEquals(valueOf(5), list.get(5)); + assertEquals(valueOf(9), list.get(list.size() - 1)); + } + + } + + public static class OfNullableTest { + + private List collection = Arrays.asList("A", "B", "C"); + private Map map = new HashMap<>() {{ + put("A", 10); + put("C", 30); + }}; + + private Stream getStreamWithOfNullable() { + return collection.stream() + .flatMap(s -> Stream.ofNullable(map.get(s))); + } + + private Stream getStream() { + return collection.stream() + .flatMap(s -> { + Integer temp = map.get(s); + return temp != null ? Stream.of(temp) : Stream.empty(); + }); + } + + private List testOfNullableFrom(Stream stream) { + List list = stream.collect(Collectors.toList()); + + assertEquals(2, list.size()); + + assertEquals(valueOf(10), list.get(0)); + assertEquals(valueOf(30), list.get(list.size() - 1)); + + return list; + } + + @Test + public void testOfNullable() { + + assertEquals( + testOfNullableFrom(getStream()), + testOfNullableFrom(getStreamWithOfNullable()) + ); + + } + + } + +}