Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class exercise1 {
- public static void main(String args[]) {
- Scanner scan = new Scanner(System.in);
- String command = scan.nextLine();
- HashMap<String, Integer> users = new HashMap<>();
- String username;
- while (!command.equals("Log out")) {
- username = command.split(": ")[1];
- if (command.contains("New follower: ")) {
- if (!users.containsKey(username)) {
- users.put(username, 0);
- }
- } else if (command.contains("Like: ")) {
- int likes = Integer.parseInt(command.split(": ")[2]);
- if (!users.containsKey(username)) {
- users.put(username, likes);
- } else {
- users.put(username, users.get(username) + likes);
- }
- } else if (command.contains("Comment: ")) {
- if (!users.containsKey(username)) {
- users.put(username, 1);
- } else {
- users.put(username, users.get(username) + 1);
- }
- } else if (command.contains("Blocked: ")) {
- if (!users.containsKey(username)) {
- System.out.println(String.format("%s doesn't exist.", username));
- } else {
- users.remove(username);
- }
- }
- command = scan.nextLine();
- }
- HashMap<String, Integer> sortedUsers = users.entrySet()
- .stream()
- .sorted(Collections.reverseOrder(Map.Entry.<String, Integer>comparingByValue())
- .thenComparing(Map.Entry.comparingByKey()))
- .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
- (newValue, oldValue) -> oldValue, LinkedHashMap::new));
- System.out.println(String.format("%d followers", sortedUsers.size()));
- sortedUsers.entrySet().forEach(entry -> {
- System.out.println(entry.getKey() + ": " + entry.getValue());
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment