Advertisement
damesova

Courses [Mimi]

Mar 16th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class _06_Courses {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         Map<String, List<String>> courseByStudents = new LinkedHashMap<>();
  8.  
  9.         String input = "";
  10.         while (!"end".equals(input = scanner.nextLine())) {
  11.             String[] line = input.split(" : ");
  12.  
  13.             courseByStudents.putIfAbsent(line[0], new ArrayList<>());
  14.             courseByStudents.get(line[0]).add(line[1]);
  15.             Collections.sort(courseByStudents.get(line[0]));
  16.         }
  17.  
  18.         courseByStudents
  19.                 .entrySet()
  20.                 .stream()
  21.                 .sorted((x, y) -> Integer.compare(y.getValue().size(), x.getValue().size()))
  22.                 .forEach(entry -> {
  23.                     System.out.println(String.format("%s: %d", entry.getKey(), entry.getValue().size()));
  24.                     entry.getValue().forEach(e -> {
  25.                         System.out.println("-- " + e);
  26.                     });
  27.                 });
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement