Advertisement
mirozspace

Courses

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