题解 | #简单错误记录#

简单错误记录

https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
	  
        LinkedHashMap<String, Integer> hashmap = new LinkedHashMap<>();
        while (in.hasNextLine()) {
            String pathAndNum = in.nextLine();
            String[] arr1 = pathAndNum.split(" ");
            // 获取文件名
            String[] arr2 = arr1[0].split("\\\\");
            // 取文件名后16位
            String fileName = arr2[arr2.length - 1];
            if (fileName.length() > 16) {
                fileName = fileName.substring(fileName.length() - 16);
            }

            String key = fileName + " " + arr1[1];
            if (hashmap.get(key) == null) {
                hashmap.put(key, 1);
            } else {
                hashmap.put(key, hashmap.get(key) + 1);
            }
        }

        List<Entry<String, Integer>> list = new ArrayList<>(hashmap.entrySet());
        if (list.size() > 8) {
            for (int i = list.size() - 8 ; i < list.size(); i++) {
                System.out.println(list.get(i).getKey() + " " + list.get(i).getValue());
            }
        } else {
            for (int i = 0 ; i < list.size(); i++) {
                System.out.println(list.get(i).getKey() + " " + list.get(i).getValue());
            }
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务