我有这个例子。我已从本教程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: [", "]"))));
我能以某种方式将这两个陈述合二为一吗?我需要按作者分组。标题和类型应该是一个字符串,用逗号和喜欢的总和分隔。
类BlogPost
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;
怎么做?
答案 0 :(得分:0)
我做到了。有用。但我相信这不是最好的解决方案。
我创建了一个新类BlogPostGroup
import java.util.Objects;
public class BlogPostGroup {
String titles;
String author;
String types;
int likes;
public BlogPostGroup(String titles, String author, String types, int likes) {
this.titles = titles;
this.author = author;
this.types = types;
this.likes = likes;
}
public String getTitles() {
return titles;
}
public void setTitles(String titles) {
this.titles = titles;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTypes() {
return types;
}
public void setTypes(String types) {
this.types = types;
}
public int getLikes() {
return likes;
}
public void setLikes(int likes) {
this.likes = likes;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BlogPostGroup that = (BlogPostGroup) o;
return likes == that.likes &&
Objects.equals(titles, that.titles) &&
Objects.equals(author, that.author) &&
Objects.equals(types, that.types);
}
@Override
public int hashCode() {
return Objects.hash(titles, author, types, likes);
}
@Override
public String toString() {
return "BlogPostGroup{" +
"titles='" + titles + '\'' +
", author='" + author + '\'' +
", types='" + types + '\'' +
", likes=" + likes +
'}';
}
}
更改了主类:
import java.util.*;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.mapping;
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);
Map<Integer, List<BlogPostGroup>> mapPostGroup = new HashMap<>();
for (Map.Entry<Integer, List<BlogPost>> entry :map.entrySet()) {
List<BlogPost> blogPosts = entry.getValue();
Map<String, Integer> postsLikesSum = blogPosts.stream().collect(groupingBy(BlogPost::getAuthor, Collectors.summingInt(BlogPost::getLikes)));
Map<String, String> postsTitles = blogPosts.stream().collect(groupingBy(BlogPost::getAuthor, mapping(BlogPost::getTitle, joining(","))));
Map<String, String> postsTypes = blogPosts.stream().collect(groupingBy(BlogPost::getAuthor, mapping(BlogPost::getType, joining(","))));
List<BlogPostGroup> blogPostGroups = new ArrayList<>();
blogPosts.forEach(
p -> blogPostGroups.add(
new BlogPostGroup(
postsTitles.get(p.getAuthor()),
p.getAuthor(),
postsTypes.get(p.getAuthor()),
postsLikesSum.get(p.getAuthor())
)
)
);
// delete duplicates
Set<BlogPostGroup> blogPostGroupSet = new HashSet<>(blogPostGroups);
blogPostGroups.clear();
blogPostGroups.addAll(blogPostGroupSet);
// sort by likes DESC order
blogPostGroups.sort((p1, p2) -> p2.likes - p1.likes );
mapPostGroup.put(entry.getKey(), blogPostGroups);
}
System.out.println(mapPostGroup);
}
}
结果如下:
{2017=[BlogPostGroup{titles='title1,title2', author='author1', types='Guide,News', likes=25}, BlogPostGroup{titles='title3,title4', author='author2', types='News,Review', likes=15}], 2018=[BlogPostGroup{titles='title7,title8', author='author2', types='News,Review', likes=12}, BlogPostGroup{titles='title5,title6', author='author1', types='Guide,News', likes=5}]}
如何做得更好?更短,更容易阅读。