Advertisement
Krassi_Daskalova

Courses Maps

Jul 26th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6. public class Courses {
  7. public static void main(String[] args) throws IOException {
  8. BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
  9. Map<String, List<String>> courses = new LinkedHashMap<>();
  10. String command;
  11. while (!"end".equals(command = read.readLine())) {
  12.  
  13. String[] info = command.split(" : ");
  14.  
  15. courses.putIfAbsent(info[0], new ArrayList<>());
  16. courses.get(info[0]).add(info[1]);
  17.  
  18. }
  19. courses.entrySet().stream().sorted((course, num) -> Integer.compare(num.getValue().size(), course.getValue().size())).forEach(entry -> {
  20. System.out.println(String.format("%s: %d", entry.getKey(), entry.getValue().size()));
  21. Collections.sort(entry.getValue());
  22. entry.getValue().forEach(name -> System.out.println("-- " + name));
  23. });
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement