Guest User

Untitled

a guest
Nov 3rd, 2018
355
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package AssociativeArrays_07.Excersise;
  2.  
  3. import java.util.*;
  4.  
  5.  
  6. public class Courses_06 {
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.         Scanner sc = new Scanner(System.in);
  11.  
  12.         String command = sc.nextLine();
  13.         Map<String, ArrayList<String>> courseAndName = new LinkedHashMap<>();
  14.  
  15.         while (!command.equals("end")) {
  16.  
  17.                 String[] token = command.split(":");
  18.                 String course = token[0];
  19.                 String name = token[1];
  20.  
  21.                 courseAndName.putIfAbsent(course, new ArrayList<>());
  22.                 courseAndName.get(course).add(name);
  23.  
  24.             command = sc.nextLine();
  25.         }
  26.  
  27.         courseAndName
  28.                 .entrySet()
  29.                 .stream()
  30.                 .sorted(Map.Entry.<String, ArrayList<String>>comparingByValue(Comparator.comparingInt(ArrayList::size))
  31.                         .reversed()
  32.                         .thenComparing(Map.Entry.comparingByKey()))
  33.                 .forEach(entry -> {
  34.                     String temp = String.valueOf(entry.getValue().size());
  35.  
  36.                     System.out.println
  37.                             // removes " " before ":"
  38.                                     (String.join("", entry.getKey().trim(), ": ", temp));
  39.  
  40.                     entry.getValue()
  41.                             .stream()
  42.                             .sorted(Comparator.comparing(String::new, Comparator.nullsFirst(String.CASE_INSENSITIVE_ORDER)))
  43.                             .forEach(e -> System.out.printf("--%s%n", e));
  44.                 });
  45.     }
  46. }
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.

×