Guest User

bkkk1

a guest
Dec 15th, 2018
66
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3. public class p06_Courses {
  4. public static void main(String[] args) {
  5.  
  6. Scanner sc = new Scanner(System.in);
  7.  
  8. String line = sc.nextLine();
  9. Map<String, List<String>> courses = new HashMap<>();
  10. Map<String, Integer> coursesByCount = new LinkedHashMap<>();
  11.  
  12. while (!line.equals("end")){
  13. String[] tokens = line.split(" : ");
  14.  
  15. String course = tokens[0];
  16. String name = tokens[1];
  17.  
  18. courses.putIfAbsent(course, new ArrayList<>());
  19. courses.get(course).add(name);
  20.  
  21. coursesByCount.putIfAbsent(course, 0);
  22. coursesByCount.put(course, coursesByCount.get(course) + 1);
  23.  
  24. line = sc.nextLine();
  25. }
  26.  
  27. coursesByCount.entrySet().stream()
  28. .sorted(Map.Entry.<String, Integer>comparingByValue()
  29. .reversed())
  30. .forEach(pair-> {
  31. System.out.printf("%s: %d%n", pair.getKey(), pair.getValue());
  32. courses.get(pair.getKey()).stream()
  33. .sorted((a, b) -> a.compareTo(b))
  34. .forEach(x -> System.out.println("-- " + x));
  35. });
  36.  
  37.  
  38.  
  39.  
  40.  
  41. }
  42. }
RAW Paste Data