这段代码很简单:
Map<String, List<BlogPost>> postsPerType = posts.stream()
.collect(groupingBy(BlogPost::getType));
BlogPost 在哪里:
class BlogPost {
String title;
String author;
String type;
int likes;
// getter setter
}
如果 BlogPost 有一些其他类作为字段,我想按这些字段之一进行分组,例如
class BlogPost {
String title;
String author;
Description description;
int likes;
// getter setter
}
class Description {
String part1;
String type;
// getter setter
}
我如何访问说
myBlogPost.getDescription().getType()
在 lambda 表达式中?
伪代码:
Map<String, List<BlogPost>> postsPerType = posts.stream()
.collect(groupingBy(BlogPost::getDescription::getType));
编辑:groupingBy 指的是:
import static java.util.stream.Collectors.groupingBy;
慕慕森
一只斗牛犬
相关分类