Code Explainers

Code explainers tagged #collectors

java
public List<String> collectTagsFromArticles(List<Article> articles) {
    return articles.stream()
        .flatMap(article -> article.getTags().stream())
        .map(String::trim)

Flattening nested data with Java streams

streams flatmap collectors
Intermediate 7 steps
java
public Map<Long, List<Order>> ordersByCustomer(List<Order> orders) {
    return orders.stream()
            .collect(Collectors.groupingBy(Order::getCustomerId));
}

Grouping streams with Java Collectors

streams grouping collectors
Intermediate 5 steps
java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

Three ways to deduplicate a list by key in Java

streams deduplication collectors
Intermediate 6 steps