Created
February 2, 2015 09:48
-
-
Save oreillyross/ecc36e42c975beed8d14 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class Article { | |
private final String title; | |
private final String author; | |
private final List<String> tags; | |
private Article(String title, String author, List<String> tags) { | |
this.title = title; | |
this.author = author; | |
this.tags = tags; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public String getAuthor() { | |
return author; | |
} | |
public List<String> getTags() { | |
return tags; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Set<String> getDistinctTags() { | |
return articles.stream() | |
.flatMap(article -> article.getTags().stream()) | |
.collect(Collectors.toSet()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Map<String, List<Article>> groupByAuthor() { | |
return articles.stream() | |
.collect(Collectors.groupingBy(Article::getAuthor)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Article> getAllJavaArticles() { | |
return articles.stream() | |
.filter(article -> article.getTags().contains("Java")) | |
.collect(Collectors.toList()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Article> getAllJavaArticles() { | |
List<Article> result = new ArrayList<>(); | |
for (Article article : articles) { | |
if (article.getTags().contains("Java")) { | |
result.add(article); | |
} | |
} | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Optional<Article> getFirstJavaArticle() { | |
return articles.stream() | |
.filter(article -> article.getTags().contains("Java")) | |
.findFirst(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Set<String> getDistinctTags() { | |
Set<String> result = new HashSet<>(); | |
for (Article article : articles) { | |
result.addAll(article.getTags()); | |
} | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Article getFirstJavaArticle() { | |
for (Article article : articles) { | |
if (article.getTags().contains("Java")) { | |
return article; | |
} | |
} | |
return null; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Map<String, List<Article>> groupByAuthor() { | |
Map<String, List<Article>> result = new HashMap<>(); | |
for (Article article : articles) { | |
if (result.containsKey(article.getAuthor())) { | |
result.get(article.getAuthor()).add(article); | |
} else { | |
ArrayList<Article> articles = new ArrayList<>(); | |
articles.add(article); | |
result.put(article.getAuthor(), articles); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment