Advertisement
petar088

Untitled

Jul 19th, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. package Fundamentals.Final_Exam._14_04_2018;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Practice_Sessions {
  7. public static void main(String[] args) {
  8. Scanner sc = new Scanner(System.in);
  9.  
  10. Map<String, List<String>> roadRacers = new TreeMap<>();
  11.  
  12.  
  13. String input = sc.nextLine() ;
  14.  
  15. while (!input.equals("END")){
  16. String[] array = input.split("->");
  17.  
  18. if(array[0].equals("Add")){
  19. String road = array[1];
  20. String racer = array[2];
  21.  
  22. roadRacers.putIfAbsent(road, new ArrayList<>());
  23. roadRacers.get(road).add(racer);
  24.  
  25. }else if(array[0].equals("Move")){
  26. String oldRoad = array[1];
  27. String racer = array[2];
  28. String newRoad = array[3];
  29.  
  30. if(roadRacers.get(oldRoad).contains(racer)){
  31. roadRacers.get(oldRoad).remove(racer);
  32. roadRacers.get(newRoad).add(racer);
  33.  
  34. }
  35.  
  36. }else if(array[0].equals("Close")){
  37. String roadToClose = array[1];
  38. roadRacers.remove(roadToClose);
  39.  
  40. }
  41. input = sc.nextLine();
  42. }
  43. Map<String,Integer> roadAndCountRacers = new LinkedHashMap<>();
  44. for (Map.Entry<String, List<String>> ss : roadRacers.entrySet()) {
  45. if(ss.getValue().size() > 0){
  46. roadAndCountRacers.put(ss.getKey(), ss.getValue().size());
  47. }
  48.  
  49. }
  50.  
  51. Map<String,Integer> sorted = new LinkedHashMap<>();
  52. sorted = roadAndCountRacers.entrySet().stream() // sorting
  53. .sorted(Map.Entry.<String,Integer>comparingByValue().reversed().thenComparing(Map.Entry.<String,Integer>comparingByKey()))
  54. .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(e1, e2)->e1,LinkedHashMap::new));
  55.  
  56. System.out.println("Practice sessions:");
  57.  
  58. for (Map.Entry<String, Integer> entry : sorted.entrySet()) {
  59. System.out.println(entry.getKey());
  60. roadRacers.get(entry.getKey()).forEach(e-> System.out.println("++"+e));
  61.  
  62.  
  63. }
  64.  
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement