From 9835081ad303155befefbd441eb86a34132ad521 Mon Sep 17 00:00:00 2001 From: alexVengrovsk Date: Thu, 9 Jun 2016 23:31:30 +0300 Subject: [PATCH] Code for the article "Java 8 Stream API Tutorial" alextrentton@gmail.com Signed-off-by: --- .../java/com/baeldung/streamApi/Product.java | 51 ++++ .../baeldung/java8/Java8StreamApiTest.java | 269 ++++++++++++++++++ 2 files changed, 320 insertions(+) create mode 100644 core-java-8/src/main/java/com/baeldung/streamApi/Product.java create mode 100644 core-java-8/src/test/java/com/baeldung/java8/Java8StreamApiTest.java diff --git a/core-java-8/src/main/java/com/baeldung/streamApi/Product.java b/core-java-8/src/main/java/com/baeldung/streamApi/Product.java new file mode 100644 index 0000000000..18f3a61904 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/streamApi/Product.java @@ -0,0 +1,51 @@ +package com.baeldung.streamApi; + +import java.util.List; +import java.util.Optional; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +/** + * Created by Alex Vengr + */ +public class Product { + + private int price; + + private String name; + + private boolean utilize; + + public Product(int price, String name) { + this(price); + this.name = name; + } + + public Product(int price) { + this.price = price; + } + + public Product() { + } + + public int getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + + public static Stream streamOf(List list) { + return (list == null || list.isEmpty()) ? Stream.empty() : list.stream(); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/java8/Java8StreamApiTest.java b/core-java-8/src/test/java/com/baeldung/java8/Java8StreamApiTest.java new file mode 100644 index 0000000000..a436b3e93f --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/java8/Java8StreamApiTest.java @@ -0,0 +1,269 @@ +package com.baeldung.java8; + +import com.baeldung.streamApi.Product; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; +import java.util.logging.Logger; +import java.util.regex.Pattern; +import java.util.stream.*; + +import static org.junit.Assert.*; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; + +/** + * Created by Alex Vengr + */ +public class Java8StreamApiTest { + + private long counter; + + private static Logger log = Logger.getAnonymousLogger(); + + private List productList; + + + @Before + public void init() { + productList = Arrays.asList(new Product(23, "potatoes"), + new Product(14, "orange"), new Product(13, "lemon"), + new Product(23, "bread"), new Product(13, "sugar")); + } + + @Test + public void checkPipeline_whenStreamOneElementShorter_thenCorrect() { + + List list = Arrays.asList("abc1", "abc2", "abc3"); + long size = list.stream().skip(1) + .map(element -> element.substring(0, 3)).count(); + assertEquals(list.size() - 1, size); + } + + @Test + public void checkOrder_whenChangeQuantityOfMethodCalls_thenCorrect() { + + List list = Arrays.asList("abc1", "abc2", "abc3"); + + counter = 0; + long sizeFirst = list.stream() + .skip(2).map(element -> { + wasCalled(); + return element.substring(0, 3); + }).count(); + assertEquals( 1, counter); + + counter = 0; + long sizeSecond = list.stream().map(element -> { + wasCalled(); + return element.substring(0, 3); + }).skip(2).count(); + assertEquals( 3, counter); + } + + @Test + public void createEmptyStream_whenEmpty_thenCorrect() { + + Stream streamEmpty = Stream.empty(); + assertEquals(0, streamEmpty.count()); + + List names = Collections.emptyList(); + Stream streamOf = Product.streamOf(names); + assertTrue(streamOf.count() == 0); + } + + @Test + public void createStream_whenCreated_thenCorrect() { + + Collection collection = Arrays.asList("a", "b", "c"); + Stream streamOfCollection = collection.stream(); + assertEquals(3, streamOfCollection.count()); + + Stream streamOfArray = Stream.of("a", "b", "c"); + assertEquals(3, streamOfArray.count()); + + String[] arr = new String[]{"a", "b", "c"}; + Stream streamOfArrayPart = Arrays.stream(arr, 1, 3); + assertEquals(2, streamOfArrayPart.count()); + + IntStream intStream = IntStream.range(1, 3); + LongStream longStream = LongStream.rangeClosed(1, 3); + Random random = new Random(); + DoubleStream doubleStream = random.doubles(3); + assertEquals(2, intStream.count()); + assertEquals(3, longStream.count()); + assertEquals(3, doubleStream.count()); + + IntStream streamOfChars = "abc".chars(); + IntStream str = "".chars(); + assertEquals(3, streamOfChars.count()); + + Stream streamOfString = Pattern.compile(", ").splitAsStream("a, b, c"); + assertEquals("a", streamOfString.findFirst().get()); + + Path path = getPath(); + Stream streamOfStrings = null; + try { + streamOfStrings = Files.lines(path, Charset.forName("UTF-8")); + } catch (IOException e) { + e.printStackTrace(); + } + assertEquals("a", streamOfStrings.findFirst().get()); + + Stream streamBuilder = Stream.builder().add("a").add("b").add("c").build(); + assertEquals(3, streamBuilder.count()); + + Stream streamGenerated = Stream.generate(() -> "element").limit(10); + assertEquals(10, streamGenerated.count()); + + Stream streamIterated = Stream.iterate(40, n -> n + 2).limit(20); + assertTrue(40 <= streamIterated.findAny().get()); + } + + @Test + public void runStreamPipeline_whenOrderIsRight_thenCorrect() { + + List list = Arrays.asList("abc1", "abc2", "abc3"); + Optional stream = list.stream() + .filter(element -> { + log.info("filter() was called"); + return element.contains("2"); + }).map(element -> { + log.info("map() was called"); + return element.toUpperCase(); + }).findFirst(); + } + + @Test + public void reduce_whenExpected_thenCorrect() { + + OptionalInt reduced = IntStream.range(1, 4).reduce((a, b) -> a + b); + assertEquals(6, reduced.getAsInt()); + + int reducedTwoParams = IntStream.range(1, 4).reduce(10, (a, b) -> a + b); + assertEquals(16, reducedTwoParams); + + int reducedThreeParams = Stream.of(1, 2, 3) + .reduce(10, (a, b) -> a + b, (a, b) -> { + log.info("combiner was called"); + return a + b; + }); + assertEquals(16, reducedThreeParams); + + int reducedThreeParamsParallel = Arrays.asList(1, 2, 3).parallelStream() + .reduce(10, (a, b) -> a + b, (a, b) -> { + log.info("combiner was called"); + return a + b; + }); + assertEquals(36, reducedThreeParamsParallel); + } + + @Test + public void collecting_whenAsExpected_thenCorrect() { + + List collectorCollection = productList.stream() + .map(Product::getName).collect(Collectors.toList()); + + assertTrue(collectorCollection instanceof List); + assertEquals(5, collectorCollection.size()); + + String listToString = productList.stream().map(Product::getName) + .collect(Collectors.joining(", ", "[", "]")); + + assertTrue(listToString.contains(",") && listToString.contains("[") && listToString.contains("]")); + + double averagePrice = productList.stream().collect(Collectors.averagingInt(Product::getPrice)); + assertTrue(17.2 == averagePrice); + + int summingPrice = productList.stream().collect(Collectors.summingInt(Product::getPrice)); + assertEquals(86, summingPrice); + + IntSummaryStatistics statistics = productList.stream() + .collect(Collectors.summarizingInt(Product::getPrice)); + assertEquals(23, statistics.getMax()); + + Map> collectorMapOfLists = productList.stream() + .collect(Collectors.groupingBy(Product::getPrice)); + assertEquals(3, collectorMapOfLists.keySet().size()); + + Map> mapPartioned = productList.stream() + .collect(Collectors.partitioningBy(element -> element.getPrice() > 15)); + assertEquals(2, mapPartioned.keySet().size()); + + } + + @Test(expected = UnsupportedOperationException.class) + public void collect_whenThrows_thenCorrect() { + Set unmodifiableSet = productList.stream() + .collect(Collectors.collectingAndThen(Collectors.toSet(), + Collections::unmodifiableSet)); + unmodifiableSet.add(new Product(4, "tea")); + } + + @Test + public void customCollector_whenResultContainsAllElementsFrSource_thenCorrect() { + Collector> toLinkedList = + Collector.of(LinkedList::new, LinkedList::add, + (first, second) -> { first.addAll(second); return first; }); + + LinkedList linkedListOfPersons = productList.stream().collect(toLinkedList); + assertTrue(linkedListOfPersons.containsAll(productList)); + } + + @Test + public void parallelStream_whenWorks_thenCorrect() { + Stream streamOfCollection = productList.parallelStream(); + boolean isParallel = streamOfCollection.isParallel(); + boolean haveBigPrice = streamOfCollection.map(product -> product.getPrice() * 12) + .anyMatch(price -> price > 200); + assertTrue(isParallel && haveBigPrice); + } + + @Test + public void parallel_whenIsParallel_thenCorrect() { + IntStream intStreamParallel = + IntStream.range(1, 150).parallel().map(element -> element * 34); + boolean isParallel = intStreamParallel.isParallel(); + assertTrue(isParallel); + } + + @Test + public void parallel_whenIsSequential_thenCorrect() { + IntStream intStreamParallel = + IntStream.range(1, 150).parallel().map(element -> element * 34); + IntStream intStreamSequential = intStreamParallel.sequential(); + boolean isParallel = intStreamParallel.isParallel(); + assertFalse(isParallel); + } + + private Path getPath() { + Path path = null; + try { + path = Files.createTempFile(null, ".txt"); + } catch (IOException e) { + e.printStackTrace(); + } + + try (BufferedWriter writer = Files.newBufferedWriter(path)) { + writer.write("a\nb\nc"); + } catch (IOException e) { + e.printStackTrace(); + } + return path; + } + + private void wasCalled() { + counter++; + } +}