Advertisement
meteor4o

JF-Maps-Exercise-06.Courses

Jul 14th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class Courses {
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner sc = new Scanner(System.in);
  9.  
  10.         Map<String, List<String>> usersList = new LinkedHashMap<>();
  11.  
  12.         String input = sc.nextLine();
  13.  
  14.         while (!input.toLowerCase().equals("end")) {
  15.             String[] tokens = input.split("\\s:\\s");
  16.             String courseName = tokens[0];
  17.             String user = tokens[1];
  18.             int counter = 0;
  19.  
  20.             if (usersList.containsKey(courseName)) {
  21.                 usersList.get(courseName).add(user);
  22.  
  23.             } else {
  24.                 usersList.put(courseName, new ArrayList<>());
  25.                 usersList.get(courseName).add(user);
  26.             }
  27.  
  28.             input = sc.nextLine();
  29.         }
  30.  
  31.         usersList.entrySet().stream().sorted((e1, e2) -> Integer.compare(e2.getValue().size(), e1.getValue().size()))
  32.                 .forEach(e -> {
  33.                     System.out.println(String.format("%s: %d", e.getKey(), e.getValue().size()));
  34.                     Collections.sort(e.getValue());
  35.                     e.getValue().forEach(a -> System.out.println("-- " + a));
  36.                 });
  37.  
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement