Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AssociativeArrays_Exsercise;
- import java.util.*;
- public class Courses {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, List<String>> courses = new LinkedHashMap<>();
- String input = scanner.nextLine();
- while (!input.equals("end")) {
- String[] tokens = input.split(" : ");
- String course = tokens[0];
- String student = tokens[1];
- courses.putIfAbsent(course,new ArrayList<>());
- courses.get(course).add(student);
- input = scanner.nextLine();
- }
- courses.entrySet().stream().sorted((f,s) -> Integer.compare(s.getValue().size(),f.getValue().size())
- ).forEach(c -> {
- System.out.printf("%s: %d%n",c.getKey(),c.getValue().size());
- c.getValue().sort((s,v) -> s.compareTo(v));
- for (Object name : c.getValue()) {
- System.out.println("-- " + name);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement