Advertisement
Krassi_Daskalova

Courses_Maps

Jul 26th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Courses {
  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 command = scanner.nextLine();
  9. while (!command.equals("end")) {
  10.  
  11. String[] info = command.split(" : ");
  12.  
  13. courses.putIfAbsent(info[0], new ArrayList<>());
  14. courses.get(info[0]).add(info[1]);
  15.  
  16. command = scanner.nextLine();
  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