Guest User

Untitled

a guest
Oct 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. class BlogPost {
  2. public String title;
  3. public String author;
  4. public String type;
  5.  
  6. BlogPost(String title, String author, String type){
  7. this.author = author;
  8. this.title = title;
  9. this.type = type;
  10. }
  11.  
  12.  
  13. public static void main(String... args) {
  14. BlogPost p0 = new BlogPost("Title0", "Author0", "type0");
  15. BlogPost p1 = new BlogPost("Title1", "Author1", "type1");
  16. BlogPost p2 = new BlogPost("Title2", "Author2", "type2");
  17. BlogPost p3 = new BlogPost("Title3", "Author2", "type1");
  18. BlogPost p4 = new BlogPost("Title4", "Author0", "type2");
  19. BlogPost p5 = new BlogPost("Title5", "Author0", "type2");
  20.  
  21. List<BlogPost> posts = Arrays.asList(p0, p1, p2, p3, p4, p5);
  22.  
  23. Map<String, List<BlogPost>> grouped = posts.stream().collect(groupingBy(p -> p.author));
  24.  
  25. Set<BlogPost> distinct = grouped.values().stream().map(ps -> ps.get(0)).collect(toSet());
  26.  
  27. System.out.println(distinct);
  28.  
  29. }
  30.  
  31. }
Add Comment
Please, Sign In to add comment