Guest User

Untitled

a guest
Apr 1st, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class CharArrToIntArr {
  4.     public static void main(String[] args) {
  5.         LinkedHashMap<String, LinkedHashMap<String, Integer>> countryMap = new LinkedHashMap<>();
  6.         countryMap.put("Bulgaria", new LinkedHashMap<>());
  7.         countryMap.get("Bulgaria").put("Veliko Turnovo", 1);
  8.         countryMap.get("Bulgaria").put("Stara Zagora", 2);
  9.         countryMap.put("UK", new LinkedHashMap<>());
  10.         countryMap.get("UK").put("London", 4);
  11.         countryMap.put("Italy", new LinkedHashMap<>());
  12.         countryMap.get("Italy").put("Rome", 3);
  13.  
  14.         LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
  15.         for (String country : countryMap.keySet()) {
  16.             LinkedHashMap<String, Integer> citymap = countryMap.get(country);
  17.             Set<String> cities = citymap.keySet();
  18.             int population = 0;
  19.             for (String city : cities) {
  20.                 population += citymap.get(city);
  21.             }
  22.             map.put(country, population);
  23.         }
  24.  
  25.         ArrayList<String> countries = new ArrayList<>();
  26.         map.entrySet().stream().sorted((a, b) -> b.getValue().compareTo(a.getValue()))
  27.                 .forEach(a -> countries.add(a.getKey()));
  28.         for (String country : countries) {
  29.             System.out.println(country + " (total population: " + map.get(country) + ")");
  30.             LinkedHashMap<String, Integer> citymap = countryMap.get(country);
  31.             citymap.entrySet().stream().sorted((city1, city2) -> city2.getValue().compareTo(city1.getValue()))
  32.                     .forEach(city -> System.out.println("=>" + city.getKey() + ": " + city.getValue()));
  33.  
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment