Advertisement
SPDG57

Followers - Maps, sorting with lambda - descending

Nov 30th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 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.     public static void main(String[] args) {
  21.         Scanner scanner = new Scanner(System.in);
  22.  
  23.         //comments - likes
  24.         Map<String, int[]> record = new LinkedHashMap<>();
  25.  
  26.         String input = "";
  27.         while (!"Log out".equals(input = scanner.nextLine())){
  28.             String[] tokens = input.split(": ");
  29.             String command = tokens[0];
  30.             String[] token;
  31.             switch (command){
  32.                 case "New follower":
  33.                     String username = tokens[1];
  34.                     if(!record.containsKey(username)){
  35.                         int[] userData = {0, 0};
  36.                         record.put(username, userData);
  37.                     }
  38.                     break;
  39.                 case "Like":
  40.                     String user = tokens[1];
  41.                     int likes = Integer.parseInt(tokens[2]);
  42.                     if(!record.containsKey(user)){
  43.                         int[] dataLikes = {0, likes};
  44.                         record.put(user, dataLikes);
  45.                     }else {
  46.                         int[] dataLikes = {record.get(user)[0], record.get(user)[1] + likes};
  47.                         record.put(user, dataLikes);
  48.                     }
  49.                     break;
  50.                 case "Comment":
  51.                     String uname = tokens[1];
  52.                     if(!record.containsKey(uname)){
  53.                         int[] dataComments = {0, 1};
  54.                         record.put(uname, dataComments);
  55.                     }else{
  56.                         int[] dataLikes = {record.get(uname)[0] + 1, record.get(uname)[1]};
  57.                         record.put(uname, dataLikes);
  58.                     }
  59.                     break;
  60.                 case "Blocked":
  61.                     String blockedUser = tokens[1];
  62.                     if(!record.containsKey(blockedUser)){
  63.                         System.out.println(blockedUser + " doesn't exist.");
  64.                     }else {
  65.                         record.remove(blockedUser);
  66.                     }
  67.                     break;
  68.             }
  69.         }
  70.         System.out.println(record.size() + " followers");
  71.  
  72.         record = record
  73.                 .entrySet()
  74.                 .stream()
  75.                 .sorted(Map.Entry.comparingByKey((Comparator.reverseOrder())))
  76.                 .sorted((a,b) ->{
  77.                    return b.getValue()[1] > a.getValue()[1] ? 0 : -1;
  78.                         })
  79.                         .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
  80.                                 (newVal, oldVal) -> oldVal, LinkedHashMap::new));
  81.  
  82.         for (Map.Entry<String, int[]> user : record.entrySet()) {
  83.             int total = user.getValue()[0] + user.getValue()[1];
  84.             System.out.printf("%s: %d\n", user.getKey(), total);
  85.         }
  86.  
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement