desislava_topuzakova

08. Company Users

Nov 5th, 2021
2,413
0
Never
11
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. String input = scanner.nextLine();
  8.  
  9. LinkedHashMap<String, List<String>> companies = new LinkedHashMap<>();
  10.  
  11. while (!input.equals("End")) {
  12. String [] data = input.split(" -> ");
  13. if(companies.containsKey(data[0])) {
  14. List<String> employees = companies.get(data[0]);
  15. if (!employees.contains(data[1]))
  16. employees.add(data[1]);
  17. } else {
  18. List<String> employees = new ArrayList<>();
  19. employees.add(data[1]);
  20. companies.put(data[0], employees);
  21. }
  22. input = scanner.nextLine();
  23. }
  24.  
  25. companies.entrySet().stream().sorted(Map.Entry.comparingByKey())
  26. .forEach(company -> {
  27. System.out.println(company.getKey());
  28. company.getValue().forEach(employee -> System.out.println("-- " + employee));
  29. });
  30. }
  31. }
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment