Last active
August 29, 2015 14:10
-
-
Save rupertbates/a6bba7123e621201c775 to your computer and use it in GitHub Desktop.
getContributorCounts method in an imperative style
This file contains 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<ContributorCount> getContributorCounts() { | |
List<ArticleView> articleViews = getArticleViews(); | |
//First iterate over the articles counting the occurrences of | |
//each contributor | |
HashMap<String, Integer> contributorCounts = new HashMap<String, Integer>(); | |
for (ArticleView articleView : articleViews) { | |
Integer count = contributorCounts.get(articleView.contributor); | |
if (count == null) | |
contributorCounts.put(articleView.contributor, 1); | |
else | |
contributorCounts.put(articleView.contributor, count + 1); | |
} | |
//Now iterate over the results filtering out the contributors who | |
//only appear once and converting the results to the required output type | |
List<ContributorCount> result = new ArrayList<ContributorCount>(); | |
for (String contributor : contributorCounts.keySet()) { | |
int count = contributorCounts.get(contributor); | |
if (count > 1) | |
result.add(new ContributorCount(contributor, contributorCounts.get(contributor))); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment