Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AssociativeArraysExercise;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class CompanyUsers {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String line = reader.readLine();
- Map<String, List<String>> companyNames = new TreeMap<>();
- while (!line.equals("end")){
- String[] input = line.split(" -> ");
- String company = input[0];
- String name = input[1];
- if (!companyNames.containsKey(company)){
- companyNames.put(company, new ArrayList<>());
- if (!companyNames.get(company).contains(name)){
- companyNames.get(company).add(name);
- }
- }
- line = reader.readLine();
- }
- companyNames.entrySet().stream().sorted((e1, e2) -> {
- int x1 = e1.getValue().size();
- int x2 = e2.getValue().size();
- return Integer.compare(x1, x2);
- }).forEach(e -> {
- System.out.println(e.getKey());
- Collections.sort(e.getValue());
- for (String names : e.getValue()) {
- System.out.println("-- " + names);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement