Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class temp_Advanced {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int count = Integer.parseInt(scanner.nextLine());
- Map<String, Map<String, List<String>>> map = new LinkedHashMap<>();
- for (int i = 0; i < count; i++) {
- String[] line = scanner.nextLine().split("\\s+");
- String continets = line[0];
- String country = line[1];
- String cities = line[2];
- if (!map.containsKey(continets)) {
- map.put(continets, new LinkedHashMap<>());
- map.get(continets).put(country, new ArrayList<>());
- map.get(continets).get(country).add(cities);
- } else if (!map.get(continets).containsKey(country)) {
- map.get(continets).put(country, new ArrayList<>());
- map.get(continets).get(country).add(cities);
- } else {
- map.get(continets).get(country).add(cities);
- }
- }
- for (Map.Entry<String, Map<String, List<String>>> entry : map.entrySet()) {
- System.out.printf("%s:%n", entry.getKey());
- for (Map.Entry<String, List<String>> a : entry.getValue().entrySet()) {
- System.out.printf(" %s -> ", a.getKey());
- for (int i = 0; i < a.getValue().size(); i++) {
- if (i == a.getValue().size() - 1) {
- System.out.println(a.getValue().get(i));
- } else {
- System.out.print(a.getValue().get(i) + ", ");
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement