Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- public class Courses {
- public static void main(String[] args) throws IOException {
- BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
- Map<String, List<String>> data = new LinkedHashMap<String, List<String>>();
- String command = bf.readLine();
- while(!command.equals("end")){
- String input[] = command.split("[:]+");
- String course = input[0].trim();
- String student = input[1].trim();
- if(!data.containsKey(course)){
- data.put(course, new ArrayList<String>());
- data.get(course).add(student);
- }else{
- data.get(course).add(student);
- }
- command = bf.readLine();
- }
- data.entrySet().stream()
- .sorted((a,b) -> {
- return Integer.compare(b.getValue().size(), a.getValue().size()); // Sorting by size descendingly!
- })
- .forEach(a -> {
- System.out.println(a.getKey() + ": " + a.getValue().size()); // Printing the course
- a.getValue().stream().sorted().forEach(c -> System.out.println("-- "+ c)); // Streaming and sorting
- }); // people in the course!
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement