Advertisement
N_Damyanov

Courses

Nov 8th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. package MapsLambda;
  2.  
  3. import java.util.*;
  4.  
  5. public class Courses {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         LinkedHashMap<String, List<String>> courses = new LinkedHashMap<>();
  10.  
  11.         String[] input = scanner.nextLine().split("\\s+:\\s+");
  12.  
  13.         while (!input[0].equals("end")) {
  14.  
  15.             if (!courses.containsKey(input[0])) {
  16.                 courses.put(input[0], new ArrayList<>());
  17.                 courses.get(input[0]).add(input[1]);
  18.             } else {
  19.                 courses.get(input[0]).add(input[1]);
  20.             }
  21.             input = scanner.nextLine().split("\\s+:\\s+");
  22.         }
  23.  
  24.         courses.entrySet().stream()
  25.                 .sorted((a, b) -> b.getValue().size() - a.getValue().size())
  26.                 .forEach(entry -> {
  27.                     System.out.printf("%s: %d%n", entry.getKey(), entry.getValue().size());
  28.                     Collections.sort(entry.getValue());
  29.                     for (int i = 0; i < entry.getValue().size(); i++) {
  30.                         System.out.printf(" -- %s%n", entry.getValue().get(i));
  31.                     }
  32.                 });
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement