Skip to content

Instantly share code, notes, and snippets.

@thegabriele97
Created May 19, 2018 16:17
Show Gist options
  • Save thegabriele97/2d5025ea0fa119e0eab1afd641c58867 to your computer and use it in GitHub Desktop.
Save thegabriele97/2d5025ea0fa119e0eab1afd641c58867 to your computer and use it in GitHub Desktop.
/**
* returns a list of strings with format
* {@code "### - XXXXXX"}, where
* {@code ###} represents the number of schools (not branches)
* and {@code XXXXXX} represents the name of the municipality.
* If a school has more than one branch in a municipality
* it must be counted only once.
*
* @return a collection of strings with the counts
*/
public Collection<String> countSchoolsPerMunicipality(){
return listOfBranch.values()
.stream()
.collect(groupingBy(Branch::getMunicipality, counting()))
.entrySet()
.stream()
.map(e -> e.getValue() + " - " + e.getKey().getName())
.collect(toList());
}
/**
* returns a list of strings with format
* {@code "### - XXXXXX"}, where
* {@code ###} represents the number of schools (not branches)
* and {@code XXXXXX} represents the name of the community.
* They are sorted by descending number of schools.s
* The list must contain only schools having at least
* a branch in a municipality part of a community.
*
* @return a collection of strings with the counts
*/
public List<String> countSchoolsPerCommunity(){
return listOfBranch.values()
.stream()
.filter(b -> ((Branch)b).getMunicipality().getCommunity().isPresent())
.collect(groupingBy(b -> ((Branch)b).getMunicipality().getCommunity(), counting()))
.entrySet()
.stream()
.sorted(Comparator.comparing(Map.Entry::getValue, Comparator.reverseOrder()))
.map(e -> e.getValue() + " - " + e.getKey().get().getName())
.collect(toList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment