Advertisement
SPDG57

Followers - 2nd solution, lambdas, using two lists

Dec 5th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javafx.util.Pair;
  4.  
  5. import javax.xml.crypto.dsig.keyinfo.KeyValue;
  6. import java.lang.reflect.Array;
  7. import java.security.KeyStore;
  8. import java.text.Collator;
  9. import java.text.DecimalFormat;
  10. import java.util.*;
  11. import java.util.function.Predicate;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14. import java.util.stream.Collector;
  15. import java.util.stream.Collectors;
  16.  
  17.  
  18. public class Main {
  19.  
  20.  
  21.  
  22.     public static void main(String[] args) {
  23.         Scanner scanner = new Scanner(System.in);
  24.  
  25.         Map<String, Integer> likes = new LinkedHashMap<>();
  26.         Map<String, Integer> comments = new LinkedHashMap<>();
  27.  
  28.         String input = "";
  29.         while(!"Log out".equals(input = scanner.nextLine())){
  30.             String[] tokens = input.split(": ");
  31.             String command = tokens[0];
  32.             String username = tokens[1];
  33.             switch (command){
  34.                 case "New follower":
  35.                     likes.putIfAbsent(username, 0);
  36.                     comments.putIfAbsent(username, 0);
  37.                     break;
  38.                 case "Like":
  39.                     Integer likeCount = Integer.parseInt(tokens[2]);
  40.                     if(likes.containsKey(username)){
  41.                        likes.put(username, likes.get(username) + likeCount);
  42.                     }else{
  43.                         likes.put(username, likeCount);
  44.                         comments.put(username, 0);
  45.                     }
  46.                     break;
  47.                 case "Comment":
  48.                     if(comments.containsKey(username)){
  49.                         comments.put(username, comments.get(username) + 1);
  50.                     }else{
  51.                         comments.put(username, 1);
  52.                         likes.put(username, 0);
  53.                     }
  54.                     break;
  55.                 case "Blocked":
  56.                     if(comments.containsKey(username)){
  57.                         likes.remove(username);
  58.                         comments.remove(username);
  59.                     }
  60.                     break;
  61.             }
  62.         }
  63.  
  64.         System.out.println(likes.size() + " followers");
  65.  
  66.         likes.entrySet()
  67.                 .stream()
  68.                 .sorted((a, b) -> {
  69.                     int result =  b.getValue().compareTo(a.getValue());
  70.                     if(result == 0){
  71.                         result = a.getKey().compareTo(b.getKey());
  72.                     }
  73.                     return result;
  74.                 }).forEach(user -> {
  75.             System.out.printf("%s: %d\n",
  76.                     user.getKey(),
  77.                     user.getValue() + comments.get(user.getKey()));
  78.         });
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement