Advertisement
SotirovG

CitiesByContinentAndCountry_06/JavaAdvanced

Sep 25th, 2021
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. package JavaProModule.JavaAdvanced.SetsAndMapsLab;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class CitiesByContinentAndCountry_06 {
  7.     public static void main(String[] args) {
  8.         Scanner read = new Scanner(System.in);
  9.  
  10.         Map<String,Map<String,List<String>>> data = new LinkedHashMap<>();
  11.         // we have Continent,Country,City
  12.  
  13.         int loop = Integer.parseInt(read.nextLine());
  14.  
  15.         for (int index = 0; index < loop; index++) {
  16.             String [] input = read.nextLine().split("\\s+");
  17.             String continent = input[0];
  18.             String country = input[1];
  19.             String city = input[2];
  20.  
  21.             data.putIfAbsent(continent,new LinkedHashMap<>());
  22.             data.get(continent).putIfAbsent(country,new ArrayList<>());
  23.             data.get(continent).get(country).add(city);
  24.  
  25.         }
  26.         data.forEach((key,value) -> {
  27.             System.out.println(key + ":");
  28.             value.forEach((innerKey,innerValue) -> {
  29.                 System.out.println(" " + innerKey + " -> " + String.join(", ",innerValue));
  30.             });
  31.         });
  32. /* for (Map.Entry<String, Map<String, List<String>>> entry : data.entrySet()) {
  33.             String key = entry.getKey();
  34.             Map<String, List<String>> value = entry.getValue();
  35.             System.out.println(key + ":");
  36.             value.forEach((innerKey, innerValue) -> {
  37.                 System.out.println(" " + innerKey + " -> " + String.join(", ", innerValue));
  38.             });
  39.         } for (Map.Entry<String, List<String>> entry : value.entrySet()) {
  40.                 String innerKey = entry.getKey();
  41.                 List<String> innerValue = entry.getValue();
  42.                 System.out.println(" " + innerKey + " -> " + String.join(", ", innerValue));
  43.             }*/
  44.  
  45.  
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement