Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class MeTube {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- Map<String, Integer> videoViews = new HashMap<>();
- Map<String, Integer> videosLikes = new HashMap<>();
- while (!"stats time".equals(input)) {
- if (input.contains("-")) {
- String[] tokens = input.split("-");
- String videoName = tokens[0];
- int views = Integer.parseInt(tokens[1]);
- if (!videoViews.containsKey(videoName)) {
- videoViews.put(videoName, views);
- } else {
- videoViews.put(videoName, videoViews.get(videoName) + views);
- }
- videosLikes.putIfAbsent(videoName, 0);
- } else {
- String[] tokens = input.split(":");
- if (tokens[0].equals("like")) {
- if (videosLikes.containsKey(tokens[1])) {
- videosLikes.put(tokens[1], videosLikes.get(tokens[1]) + 1);
- }
- } else {
- if (videosLikes.containsKey(tokens[1])) {
- videosLikes.put(tokens[1], videosLikes.get(tokens[1]) - 1);
- }
- }
- }
- input = scanner.nextLine();
- }
- String sortingCriteria = scanner.nextLine();
- if (sortingCriteria.equals("by views")) {
- videoViews.entrySet().stream().sorted((es1, es2) -> es2.getValue() - es1.getValue()).forEach(e -> {
- System.out.printf("%s - %d views - %d likes%n", e.getKey(), e.getValue(), videosLikes.get(e.getKey()));
- });
- } else {
- videosLikes.entrySet().stream().sorted((es1, es2) -> es2.getValue() - es1.getValue()).forEach(e ->
- System.out.printf("%s - %d views - %d likes%n", e.getKey(), videoViews.get(e.getKey()), e.getValue()));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement