这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.baeldung.streams.collectors;

import java.util.List;

public class Blog {

private String authorName;
private List<String> comments;

public Blog(String authorName, String... comments) {
this.authorName = authorName;
this.comments = List.of(comments);
}

public String getAuthorName() {
return this.authorName;
}

public List<String> getComments() {
return this.comments;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.baeldung.streams.collectors;

import static org.junit.Assert.assertEquals;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;

class CollectorImprovementUnitTest {

@Test
void givenList_whenSatifyPredicate_thenMapValueWithOccurences() {
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
void givenListOfBlogs_whenAuthorName_thenMapAuthorWithComments() {
Blog blog1 = new Blog("1", "Nice", "Very Nice");
Blog blog2 = new Blog("2", "Disappointing", "Ok", "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());
}
}