ilianrusev

Maps, Lambda and Stream API - Exercise - Courses

Mar 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.        String input = "";
  12.        Map<String, List<String>> data = new LinkedHashMap<>();
  13.  
  14.  
  15.  
  16.         System.out.println();
  17.        while (!"end".equals(input = scanner.nextLine())){
  18.            String[] list = input.split(":");
  19.            String courseName = list[0];
  20.            String studentName = list[1];
  21.  
  22.            data.putIfAbsent(courseName,new ArrayList<>());
  23.            data.get(courseName).add(studentName);
  24.        }
  25.  
  26.        data.entrySet()
  27.                .stream()
  28.                .sorted((a,b) -> Integer.compare(
  29.                        b.getValue().size(),
  30.                        a.getValue().size()))
  31.                .forEach(e -> {
  32.                    System.out.printf("%s: %d\n",e.getKey().trim(),e.getValue().size());
  33.                    e.getValue()
  34.                            .stream()
  35.                            .sorted((a1,a2) -> a1.compareTo(a2))
  36.                            .forEach(u -> System.out.printf("-- %s\n",u.trim()));
  37.                });
  38.  
  39.  
  40.     }
  41. }
Add Comment
Please, Sign In to add comment