Skip to content

Instantly share code, notes, and snippets.

@rupertbates
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rupertbates/a6bba7123e621201c775 to your computer and use it in GitHub Desktop.
Save rupertbates/a6bba7123e621201c775 to your computer and use it in GitHub Desktop.
getContributorCounts method in an imperative style
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