Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Count the number of municipalities with at least a mountain hut per each
- * province.
- *
- * @return a map with the province as key and the number of municipalities as
- * value
- */
- public Map<String, Long> countMunicipalitiesPerProvince() {
- return municipalities.values().stream().collect(groupingBy(s -> s.getProvince(), counting()));
- }
- /**
- * Count the number of mountain huts per each municipality within each province.
- *
- * @return a map with the province as key and, as value, a map with the
- * municipality as key and the number of mountain huts as value
- */
- public Map<String, Map<String, Long>> countMountainHutsPerMunicipalityPerProvince() {
- return mountainHuts.values().stream().collect(groupingBy((s) -> s.getMunicipality().getProvince(),
- groupingBy((b) -> b.getMunicipality().getName().toString(), counting())));
- }
- /**
- * Count the number of mountain huts per altitude range. If the altitude of the
- * mountain hut is not available, use the altitude of its municipality.
- *
- * @return a map with the altitude range as key and the number of mountain huts
- * as value
- */
- public Map<String, Long> countMountainHutsPerAltitudeRange() {
- return mountainHuts.values()
- .stream()
- .collect(groupingBy((s) ->{
- if(s.getAltitude().isPresent()) return this.getAltitudeRange(s.getAltitude().get());
- return this.getAltitudeRange(s.getMunicipality().getAltitude());
- }, counting()));
- }
- /**
- * Compute the total number of beds available in the mountain huts per each
- * province.
- *
- * @return a map with the province as key and the total number of beds as value
- */
- public Map<String, Integer> totalBedsNumberPerProvince() {
- return mountainHuts.values().stream().collect(groupingBy((s) -> s.getMunicipality().getProvince(), summingInt((s) -> s.getBedsNumber())));
- }
- /**
- * Compute the maximum number of beds available in a single mountain hut per
- * altitude range. If the altitude of the mountain hut is not available, use the
- * altitude of its municipality.
- *
- * @return a map with the altitude range as key and the maximum number of beds
- * as value
- */
- public Map<String, Optional<Integer>> maximumBedsNumberPerAltitudeRange() {
- return mountainHuts.values()
- .stream()
- .collect(groupingBy((s) -> {
- if(s.getAltitude().isPresent()) return this.getAltitudeRange(s.getAltitude().get());
- return this.getAltitudeRange(s.getMunicipality().getAltitude());
- }, mapping(s -> s.getBedsNumber(), maxBy(Integer::compareTo))));
- }
- /**
- * Compute the municipality names per number of mountain huts in a municipality.
- * The lists of municipality names must be in alphabetical order.
- *
- * @return a map with the number of mountain huts in a municipality as key and a
- * list of municipality names as value
- */
- public Map<Long, List<String>> municipalityNamesPerCountOfMountainHuts() {
- return mountainHuts.values().stream().collect(groupingBy(s -> s.getMunicipality().getName(), counting()))
- .entrySet().stream().sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey())).collect(groupingBy(e -> e.getValue(), mapping(e -> e.getKey(), toList())));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement