Advertisement
vdjalov

Untitled

Nov 1st, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.LinkedHashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. public class Courses {
  10.  
  11. public static void main(String[] args) throws IOException {
  12.  
  13. BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  14. Map<String, List<String>> data = new LinkedHashMap<String, List<String>>();
  15.  
  16. String command = bf.readLine();
  17.  
  18. while(!command.equals("end")){
  19. String input[] = command.split("[:]+");
  20. String course = input[0].trim();
  21. String student = input[1].trim();
  22.  
  23. if(!data.containsKey(course)){
  24. data.put(course, new ArrayList<String>());
  25. data.get(course).add(student);
  26. }else{
  27. data.get(course).add(student);
  28. }
  29. command = bf.readLine();
  30. }
  31.  
  32.  
  33. data.entrySet().stream()
  34. .sorted((a,b) -> {
  35. return Integer.compare(b.getValue().size(), a.getValue().size()); // Sorting by size descendingly!
  36. })
  37. .forEach(a -> {
  38. System.out.println(a.getKey() + ": " + a.getValue().size()); // Printing the course
  39. a.getValue().stream().sorted().forEach(c -> System.out.println("-- "+ c)); // Streaming and sorting
  40. }); // people in the course!
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement