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
This commit is contained in:
parent
e02f7825a3
commit
dd2772a387
|
@ -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<String> comments;
|
||||||
|
|
||||||
|
public Blog(String authorName) {
|
||||||
|
this.authorName = authorName;
|
||||||
|
this.comments = new LinkedList<String>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthorName() {
|
||||||
|
return this.authorName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getComments() {
|
||||||
|
return new LinkedList<String>(this.comments);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addComment(String comment) {
|
||||||
|
this.comments.add(comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFiltering() {
|
||||||
|
List<Integer> numbers = List.of(1, 2, 3, 5, 5);
|
||||||
|
|
||||||
|
Map<Integer, Long> 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<Blog> blogs = List.of(blog1, blog2);
|
||||||
|
|
||||||
|
Map<String, List<List<String>>> 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<String, List<String>> 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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue