Advertisement
nikeza

Cities by Continent and Country /lab/

Sep 10th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1.  
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4.  
  5. public class temp_Advanced {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int count = Integer.parseInt(scanner.nextLine());
  10.         Map<String, Map<String, List<String>>> map = new LinkedHashMap<>();
  11.  
  12.         for (int i = 0; i < count; i++) {
  13.             String[] line = scanner.nextLine().split("\\s+");
  14.             String continets = line[0];
  15.             String country = line[1];
  16.             String cities = line[2];
  17.             if (!map.containsKey(continets)) {
  18.                 map.put(continets, new LinkedHashMap<>());
  19.                 map.get(continets).put(country, new ArrayList<>());
  20.                 map.get(continets).get(country).add(cities);
  21.             } else if (!map.get(continets).containsKey(country)) {
  22.                 map.get(continets).put(country, new ArrayList<>());
  23.                 map.get(continets).get(country).add(cities);
  24.             } else {
  25.                 map.get(continets).get(country).add(cities);
  26.             }
  27.         }
  28.  
  29.         for (Map.Entry<String, Map<String, List<String>>> entry : map.entrySet()) {
  30.             System.out.printf("%s:%n", entry.getKey());
  31.             for (Map.Entry<String, List<String>> a : entry.getValue().entrySet()) {
  32.                 System.out.printf("  %s -> ", a.getKey());
  33.                 for (int i = 0; i < a.getValue().size(); i++) {
  34.                     if (i == a.getValue().size() - 1) {
  35.                         System.out.println(a.getValue().get(i));
  36.                     } else {
  37.                         System.out.print(a.getValue().get(i) + ", ");
  38.                     }
  39.                 }
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement