Advertisement
Guest User

Courses

a guest
Jul 9th, 2019
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. package AssociativeArrays_Exsercise;
  2.  
  3. import java.util.*;
  4.  
  5. public class Courses {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         Map<String, List<String>> courses = new LinkedHashMap<>();
  9.         String input = scanner.nextLine();
  10.         while (!input.equals("end")) {
  11.             String[] tokens = input.split(" : ");
  12.             String course = tokens[0];
  13.             String student = tokens[1];
  14.             courses.putIfAbsent(course,new ArrayList<>());
  15.             courses.get(course).add(student);
  16.  
  17.             input = scanner.nextLine();
  18.         }
  19.         courses.entrySet().stream().sorted((f,s) -> Integer.compare(s.getValue().size(),f.getValue().size())
  20.         ).forEach(c -> {
  21.             System.out.printf("%s: %d%n",c.getKey(),c.getValue().size());
  22.             c.getValue().sort((s,v) -> s.compareTo(v));
  23.             for (Object name : c.getValue()) {
  24.                 System.out.println("-- " + name);
  25.             }
  26.         });
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement