Advertisement
Guest User

6.Courses

a guest
Nov 2nd, 2018
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.String;
  3.  
  4. public class Task6 {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         Map<String, ArrayList<String>> coursePerson = new HashMap<>();
  9.         String line = scanner.nextLine();
  10.         while (!line.equals("end")) {
  11.             String[] tokens = line.split(" : ");
  12.             if (!coursePerson.containsKey(tokens[0])) {
  13.                 coursePerson.put(tokens[0], new ArrayList<>());
  14.                 coursePerson.get(tokens[0]).add(tokens[1]);
  15.             } else {
  16.                 coursePerson.get(tokens[0]).add(tokens[1]);
  17.                 Collections.sort(coursePerson.get(tokens[0]));
  18.             }
  19.             line = scanner.nextLine();
  20.         }
  21.         coursePerson.entrySet().stream().sorted((left, right) -> Integer.compare(right.getValue().size(), left.getValue().size()))
  22.                 .forEach(entry -> {
  23.             System.out.printf("%s: %d%n", entry.getKey(), entry.getValue().size());
  24.             for (int i = 0; i < entry.getValue().size(); i++) {
  25.                 System.out.println("-- " + entry.getValue().get(i));
  26.             }
  27.         });
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement