【发布时间】:2017-12-20 07:49:37
【问题描述】:
我有这个例子。我从本教程http://www.baeldung.com/java-groupingby-collector 中获取了它。 但它没有解释如何获得几个分组的参数。 例如,此代码仅获得喜欢的总和
Map<BlogPostType, Integer> likesPerType = posts.stream()
.collect(groupingBy(BlogPost::getType, summingInt(BlogPost::getLikes)));
此代码仅获取标题。
Map<BlogPostType, String> postsPerType = posts.stream()
.collect(groupingBy(BlogPost::getType,
mapping(BlogPost::getTitle, joining(", ", "Post titles: [", "]"))));
我能否以某种方式将这两个语句合二为一?我需要按作者对它们进行分组。标题和类型每一个都应该是一个字符串,用逗号和喜欢的总和分隔。
课堂博文
public class BlogPost {
String title;
String author;
String type;
int likes;
public BlogPost(String title, String author, String type, int likes) {
this.title = title;
this.author = author;
this.type = type;
this.likes = likes;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getLikes() {
return likes;
}
public void setLikes(int likes) {
this.likes = likes;
}
@Override
public String toString() {
return "BlogPost{" +
"title='" + title + '\'' +
", author='" + author + '\'' +
", type=" + type +
", likes=" + likes +
'}';
}
}
Main.java
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<Integer, List<BlogPost>> map = new HashMap<>();
map.put(2017, Arrays.asList(
new BlogPost("title1", "author1", "Guide", 10),
new BlogPost("title2", "author1", "News", 15),
new BlogPost("title3", "author2", "News", 5),
new BlogPost("title4", "author2", "Review", 10)
));
map.put(2018, Arrays.asList(
new BlogPost("title5", "author1", "Guide", 3),
new BlogPost("title6", "author1", "News", 2),
new BlogPost("title7", "author2", "News", 1),
new BlogPost("title8", "author2", "Review", 11)
));
System.out.println(map);
}
}
结果
{2017=[BlogPost{title='title1', author='author1', type=Guide, likes=10}, BlogPost{title='title2', author='author1', type=News, likes=15}, BlogPost{title='title3', author='author2', type=News, likes=5}, BlogPost{title='title4', author='author2', type=Review, likes=10}],2018=[BlogPost{title='title5', author='author1', type=Guide, likes=3}, BlogPost{title='title6', author='author1', type=News, likes=2}, BlogPost{title='title7', author='author2', type=News, likes=1}, BlogPost{title='title8', author='author2', type=Review, likes=11}]}
所以结果应该是这样的:
{2017=[BlogPost{title='title1, title2', author='author1', type='Guide,News', likes=25}, BlogPost{title='title3, title4' author='author2', type='News, Review', likes=15}],2018=[BlogPost{title='title5, title6', author='author1', type='Guide,News', likes=5}, BlogPost{title='title7, title8', author='author2', type='News, Review', likes=12}]}
最好用 Java 8 Lambda 来完成。
如果是 SQL 语句。大概是这样的:
SELECT author, title, type, SUM(likes) FROM BlogPost GROUP BY author;
如何实现?
【问题讨论】:
-
你的问题是?
-
你的问题是?你试过什么吗?做了一些研究?关于如何在 Java 中对集合的元素进行分组,有很多问题。你读过吗?
-
是的。我找到了一些很好的教程。 baeldung.com/java-groupingby-collectormkyong.com/java8/…
-
但是他们没有解释如何在一个语句中对几个参数进行分组。我实际上是从第一个链接中得到这个例子的。
-
例如 Map
likesPerType = posts.stream() .collect(groupingBy(BlogPost::getType, summingInt(BlogPost::getLikes)));我得到一张按类型分组的地图和喜欢的总和。但是我怎么能在这个声明中得到标题(用逗号分隔)和作者(也用逗号分隔)