From dd2772a3874d309c0fb46bd202d083eda0f181cb Mon Sep 17 00:00:00 2001 From: Nitish Bangera Date: Wed, 25 Jan 2017 13:45:11 +0530 Subject: [PATCH] BAEL-591 : New Stream Collectors in Java 9 (#1046) * String to char and vice versa initial article code * Junit test case for String Conversion. Deleting normal class. * Changes based on review. * BAEL-591 : New Java9 Collectors code snippet * Deleting String conversion test --- .../stream/CollectorImprovementTest.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectorImprovementTest.java diff --git a/core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectorImprovementTest.java b/core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectorImprovementTest.java new file mode 100644 index 0000000000..71ee6c6ae4 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectorImprovementTest.java @@ -0,0 +1,81 @@ +package com.baeldung.java9.language.stream; + +import org.junit.Test; + +import java.util.*; +import java.util.stream.Collectors; +import java.util.function.Function; + +import static org.junit.Assert.assertEquals; + +public class CollectorImprovementTest { + private static class Blog { + private String authorName; + private List comments; + + public Blog(String authorName) { + this.authorName = authorName; + this.comments = new LinkedList(); + } + + public String getAuthorName() { + return this.authorName; + } + + public List getComments() { + return new LinkedList(this.comments); + } + + public void addComment(String comment) { + this.comments.add(comment); + } + } + + @Test + public void testFiltering() { + List numbers = List.of(1, 2, 3, 5, 5); + + Map result = numbers.stream() + .filter(val -> val > 3).collect(Collectors.groupingBy( + Function.identity(), Collectors.counting())); + + assertEquals(1, result.size()); + + result = numbers.stream().collect(Collectors.groupingBy( + Function.identity(), Collectors.filtering(val -> val > 3, + Collectors.counting()))); + + assertEquals(4, result.size()); + } + + @Test + public void testFlatMapping() { + Blog blog1 = new CollectorImprovementTest.Blog("1"); + blog1.addComment("Nice"); + blog1.addComment("Very Nice"); + Blog blog2 = new CollectorImprovementTest.Blog("2"); + blog2.addComment("Disappointing"); + blog2.addComment("Ok"); + blog2.addComment("Could be better"); + List blogs = List.of(blog1, blog2); + + Map>> authorComments1 = + blogs.stream().collect(Collectors.groupingBy( + Blog::getAuthorName, Collectors.mapping( + Blog::getComments, Collectors.toList()))); + + assertEquals(2, authorComments1.size()); + assertEquals(2, authorComments1.get("1").get(0).size()); + assertEquals(3, authorComments1.get("2").get(0).size()); + + Map> authorComments2 = + blogs.stream().collect(Collectors.groupingBy( + Blog::getAuthorName, Collectors.flatMapping( + blog -> blog.getComments().stream(), + Collectors.toList()))); + + assertEquals(2, authorComments2.size()); + assertEquals(2, authorComments2.get("1").size()); + assertEquals(3, authorComments2.get("2").size()); + } +}