Advertisement
SIRAKOV4444

Untitled

Apr 9th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class practice {
  4.  
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7.  
  8. Map<String, List<String>> userEmail = new TreeMap<>();//TODO <<<<<
  9.  
  10. String input = sc.nextLine();
  11. while (!input.equals("Statistics")) {
  12. String[] commands = input.split("->");
  13. String firstCommand = commands[0];
  14. switch (firstCommand) {
  15. case "Add": {
  16. String username = commands[1];
  17. if (userEmail.containsKey(username)) {
  18. System.out.println(String.format("%s is already registered", username));
  19. } else {
  20. userEmail.put(username, new ArrayList<>());
  21. }
  22. }
  23. break;// TODO OK<<<>>>
  24. case "Send": {
  25. String username = commands[1];
  26. String email = commands[2];
  27. if (userEmail.containsKey(username)) {
  28. userEmail.get(username).add(email);
  29. //}else{
  30. //userEmail.putIfAbsent(username).add(email);
  31. }
  32. }
  33. break;
  34. case "Delete": {
  35. String username = commands[1];
  36. if (userEmail.containsKey(username)) {
  37. userEmail.remove(username);
  38. } else {
  39. System.out.println(String.format("%s not found!", username));
  40. }
  41. }
  42. break;
  43. }
  44. input = sc.nextLine();
  45. }
  46. System.out.println(String.format("Users count: %d", userEmail.size()));
  47.  
  48. userEmail
  49. .entrySet()
  50. .stream()
  51. .sorted((a, b) -> b.getValue().size() - a.getValue().size())
  52. .forEach(entry ->{
  53. System.out.println(entry.getKey());
  54. entry.getValue().forEach(email ->System.out.printf(" - %s%n",email));
  55. });
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement