Advertisement
Guest User

Untitled

a guest
May 19th, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1.     /**
  2.      * Count the number of municipalities with at least a mountain hut per each
  3.      * province.
  4.      *
  5.      * @return a map with the province as key and the number of municipalities as
  6.      *         value
  7.      */
  8.     public Map<String, Long> countMunicipalitiesPerProvince() {
  9.         return municipalities.values().stream().collect(groupingBy(s -> s.getProvince(), counting()));
  10.     }
  11.  
  12.     /**
  13.      * Count the number of mountain huts per each municipality within each province.
  14.      *
  15.      * @return a map with the province as key and, as value, a map with the
  16.      *         municipality as key and the number of mountain huts as value
  17.      */
  18.     public Map<String, Map<String, Long>> countMountainHutsPerMunicipalityPerProvince() {
  19.         return mountainHuts.values().stream().collect(groupingBy((s) -> s.getMunicipality().getProvince(),
  20.                                                         groupingBy((b) -> b.getMunicipality().getName().toString(), counting())));
  21.     }
  22.  
  23.     /**
  24.      * Count the number of mountain huts per altitude range. If the altitude of the
  25.      * mountain hut is not available, use the altitude of its municipality.
  26.      *
  27.      * @return a map with the altitude range as key and the number of mountain huts
  28.      *         as value
  29.      */
  30.     public Map<String, Long> countMountainHutsPerAltitudeRange() {
  31.         return mountainHuts.values()
  32.                 .stream()
  33.                 .collect(groupingBy((s) ->{
  34.                     if(s.getAltitude().isPresent()) return this.getAltitudeRange(s.getAltitude().get());
  35.                     return this.getAltitudeRange(s.getMunicipality().getAltitude());
  36.                 }, counting()));
  37.     }
  38.    
  39.     /**
  40.      * Compute the total number of beds available in the mountain huts per each
  41.      * province.
  42.      *
  43.      * @return a map with the province as key and the total number of beds as value
  44.      */
  45.     public Map<String, Integer> totalBedsNumberPerProvince() {
  46.         return mountainHuts.values().stream().collect(groupingBy((s) -> s.getMunicipality().getProvince(), summingInt((s) -> s.getBedsNumber())));
  47.     }
  48.  
  49.     /**
  50.      * Compute the maximum number of beds available in a single mountain hut per
  51.      * altitude range. If the altitude of the mountain hut is not available, use the
  52.      * altitude of its municipality.
  53.      *
  54.      * @return a map with the altitude range as key and the maximum number of beds
  55.      *         as value
  56.      */
  57.     public Map<String, Optional<Integer>> maximumBedsNumberPerAltitudeRange() {
  58.         return mountainHuts.values()
  59.                 .stream()
  60.                 .collect(groupingBy((s) -> {
  61.                                             if(s.getAltitude().isPresent()) return this.getAltitudeRange(s.getAltitude().get());
  62.                                             return this.getAltitudeRange(s.getMunicipality().getAltitude());
  63.                                            }, mapping(s -> s.getBedsNumber(), maxBy(Integer::compareTo))));
  64.     }
  65.  
  66.     /**
  67.      * Compute the municipality names per number of mountain huts in a municipality.
  68.      * The lists of municipality names must be in alphabetical order.
  69.      *
  70.      * @return a map with the number of mountain huts in a municipality as key and a
  71.      *         list of municipality names as value
  72.      */
  73.     public Map<Long, List<String>> municipalityNamesPerCountOfMountainHuts() {
  74.         return mountainHuts.values().stream().collect(groupingBy(s -> s.getMunicipality().getName(), counting()))
  75.                 .entrySet().stream().sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey())).collect(groupingBy(e -> e.getValue(), mapping(e -> e.getKey(), toList())));
  76.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement