Advertisement
desislava_topuzakova

Untitled

Jul 9th, 2022
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Demo {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. String input = scanner.nextLine();
  7. //име на курс -> списък с имената на студенти
  8. Map<String, List<String>> courses = new LinkedHashMap<>();
  9. while(!input.equals("end")) {
  10. //"{име на курс} : {име на човек}" -> split (" : ") -> ["{courseName}", "{personName}"]
  11. String courseName = input.split(" : ")[0];
  12. String personName = input.split(" : ")[1];
  13. //прверка имам ли такъв курс
  14. //ако нямам такъв курс
  15. if(!courses.containsKey(courseName)) {
  16. courses.put(courseName, new ArrayList<>());
  17. }
  18.  
  19. //ако курсът е нов -> връща празен списък
  20. //ако курсът е стар -> връща моментния списък с хора
  21. courses.get(courseName).add(personName);
  22.  
  23. input = scanner.nextLine();
  24. }
  25. //courseName -> List<String>
  26. courses.entrySet()
  27. .forEach(entry -> {
  28. //key: име на курса
  29. //value: списък с хората
  30. //име на курса -> бр. хората
  31. System.out.println(entry.getKey() + ": " + entry.getValue().size());
  32. entry.getValue().forEach(studentName -> System.out.println("-- " + studentName));
  33. //ascending order / нарастващ ред
  34. });
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement