Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BlogPost {
- public String title;
- public String author;
- public String type;
- BlogPost(String title, String author, String type){
- this.author = author;
- this.title = title;
- this.type = type;
- }
- public static void main(String... args) {
- BlogPost p0 = new BlogPost("Title0", "Author0", "type0");
- BlogPost p1 = new BlogPost("Title1", "Author1", "type1");
- BlogPost p2 = new BlogPost("Title2", "Author2", "type2");
- BlogPost p3 = new BlogPost("Title3", "Author2", "type1");
- BlogPost p4 = new BlogPost("Title4", "Author0", "type2");
- BlogPost p5 = new BlogPost("Title5", "Author0", "type2");
- List<BlogPost> posts = Arrays.asList(p0, p1, p2, p3, p4, p5);
- Map<String, List<BlogPost>> grouped = posts.stream().collect(groupingBy(p -> p.author));
- Set<BlogPost> distinct = grouped.values().stream().map(ps -> ps.get(0)).collect(toSet());
- System.out.println(distinct);
- }
- }
Add Comment
Please, Sign In to add comment