這段代碼很簡單: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;
添加回答
舉報
0/150
提交
取消