Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class CharArrToIntArr {
- public static void main(String[] args) {
- LinkedHashMap<String, LinkedHashMap<String, Integer>> countryMap = new LinkedHashMap<>();
- countryMap.put("Bulgaria", new LinkedHashMap<>());
- countryMap.get("Bulgaria").put("Veliko Turnovo", 1);
- countryMap.get("Bulgaria").put("Stara Zagora", 2);
- countryMap.put("UK", new LinkedHashMap<>());
- countryMap.get("UK").put("London", 4);
- countryMap.put("Italy", new LinkedHashMap<>());
- countryMap.get("Italy").put("Rome", 3);
- LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
- for (String country : countryMap.keySet()) {
- LinkedHashMap<String, Integer> citymap = countryMap.get(country);
- Set<String> cities = citymap.keySet();
- int population = 0;
- for (String city : cities) {
- population += citymap.get(city);
- }
- map.put(country, population);
- }
- ArrayList<String> countries = new ArrayList<>();
- map.entrySet().stream().sorted((a, b) -> b.getValue().compareTo(a.getValue()))
- .forEach(a -> countries.add(a.getKey()));
- for (String country : countries) {
- System.out.println(country + " (total population: " + map.get(country) + ")");
- LinkedHashMap<String, Integer> citymap = countryMap.get(country);
- citymap.entrySet().stream().sorted((city1, city2) -> city2.getValue().compareTo(city1.getValue()))
- .forEach(city -> System.out.println("=>" + city.getKey() + ": " + city.getValue()));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment