desislava_topuzakova

06. Courses

Mar 7th, 2021
707
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×