desislava_topuzakova

06. Courses

Mar 7th, 2021
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. import com.sun.security.jgss.GSSUtil;
  2.  
  3. import java.util.*;
  4.  
  5. public class demo {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.        //курс -> списък с имена на студентите
  9.         Map<String, List<String>> courses = new LinkedHashMap<>();
  10.  
  11.         String input = scanner.nextLine();
  12.  
  13.         while(!input.equals("end")) {
  14.             //{courseName} : {studentName} -> split  -> ["course", "student"]
  15.             String course = input.split(" : ")[0];
  16.             String studentName = input.split(" : ")[1];
  17.  
  18.             if(!courses.containsKey(course)){
  19.                 courses.put(course, new ArrayList<>());
  20.             }
  21.             courses.get(course).add(studentName);
  22.  
  23.             input = scanner.nextLine();
  24.         }
  25.  
  26.         courses.entrySet().stream()
  27.                 .sorted((e1, e2) -> Integer.compare(e2.getValue().size(), e1.getValue().size()))
  28.                 .forEach(e -> {
  29.                         System.out.println(e.getKey() + ": " + e.getValue().size());
  30.                         e.getValue().stream()
  31.                                 .sorted(String::compareTo)
  32.                                 .forEach(student -> System.out.println("-- " + student));
  33.                 });
  34.  
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment