SHARE
TWEET

Untitled

a guest Nov 12th, 2015 120 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import com.sun.javafx.image.IntPixelGetter;
  2. import javafx.util.Pair;
  3.  
  4. import java.util.*;
  5.  
  6. /**
  7.  * Created by user on 11/12/2015.
  8.  */
  9. public class PopulationCounter {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         Map<String, Long> populationData = new LinkedHashMap<>();
  14.         Map<String, List<Pair<String, Integer>>> cityPopulation = new LinkedHashMap<>();
  15.  
  16.         while (!scanner.hasNext("report")){
  17.             String[] currentData = scanner.nextLine().split("[|]+");
  18.  
  19.             String currentCountry = currentData[1];
  20.             String currentCity = currentData[0];
  21.             Integer currentPopulation = Integer.parseInt(currentData[2].trim());
  22.  
  23.             UpdateCountryPopulation(populationData, currentCountry, currentPopulation);
  24.             UpdateCityList(cityPopulation, currentCountry, currentCity, currentPopulation);
  25.         }
  26.  
  27.         Comparator<Map.Entry<String, Long>> customTotalPopulationComparator = new Comparator<Map.Entry<String, Long>>() {
  28.             @Override
  29.             public int compare(Map.Entry<String, Long> o1, Map.Entry<String, Long> o2) {
  30.                 return o2.getValue().compareTo(o1.getValue());
  31.             }
  32.         };
  33.  
  34.         Comparator<Pair<String, Integer>> customeCityComparator = new Comparator<Pair<String, Integer>>() {
  35.             @Override
  36.             public int compare(Pair<String, Integer> o1, Pair<String, Integer> o2) {
  37.                 return o2.getValue().compareTo(o1.getValue());
  38.             }
  39.         };
  40.  
  41.         List<Map.Entry<String, Long>> sortedCountries = new ArrayList<>(populationData.entrySet());
  42.         Collections.sort(sortedCountries, customTotalPopulationComparator);
  43.  
  44.         for (Map.Entry<String, Long> currentCountry : sortedCountries){
  45.             System.out.printf("%s (total population: %d)\n", currentCountry.getKey(), currentCountry.getValue());
  46.             Collections.sort(cityPopulation.get(currentCountry.getKey()), customeCityComparator);
  47.             for (Pair<String, Integer> currentCity : cityPopulation.get(currentCountry.getKey())){
  48.                 System.out.printf("=>%s: %d\n", currentCity.getKey(), currentCity.getValue());
  49.             }
  50.         }
  51.     }
  52.  
  53.     private static void UpdateCityList(Map<String, List<Pair<String, Integer>>> cityPopulation, String currentCountry, String currentCity, Integer currentPopulation) {
  54.         if (!cityPopulation.containsKey(currentCountry)){
  55.             cityPopulation.put(currentCountry, new ArrayList<>());
  56.         }
  57.  
  58.         cityPopulation.get(currentCountry).add(new Pair<String, Integer>(currentCity, currentPopulation));
  59.     }
  60.  
  61.     private static void UpdateCountryPopulation(Map<String, Long> populationData, String currentCountry, Integer currentPopulation) {
  62.         if (!populationData.containsKey(currentCountry)){
  63.             populationData.put(currentCountry, 0l);
  64.         }
  65.  
  66.         long countryPopulationValue = populationData.get(currentCountry) + currentPopulation;
  67.         populationData.put(currentCountry, countryPopulationValue);
  68.     }
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top