题解 | #简单错误记录#LinkedHashMap解决

简单错误记录

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            if (str != null && str.length() != 0) {
                String[] strs = str.split(" ");
                String[] fileNames = strs[0].split("\\\\");
                String fileName = fileNames[fileNames.length - 1];
                if (fileName.length() > 16) {
                    fileName = fileName.substring(fileName.length() - 16, fileName.length());
                }
                fileName = fileName + " " + strs[1];
                if (map.containsKey(fileName)) {
                    map.put(fileName, map.get(fileName) + 1);
                } else {
                    map.put(fileName, 1);
                }
            }
        }
        List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
        // Collections.reverse(list);
        // for (int i = 0; i < 8; i++) {
        //     if (list.size() > i) {
        //         Map.Entry<String, Integer> entry = list.get(list.size() - i - 1);
        //         System.out.println(entry.getKey() + " " + entry.getValue());
        //     }
        // }
        list.stream().skip(Math.max(list.size() - 8,
                                    0)).map(entry -> entry.getKey() + " " + entry.getValue()).forEach(
                                        System.out::println);
    }
}

#简单错误记录#
全部评论
linkedHashMap解决
点赞 回复 分享
发布于 2024-07-04 10:06 广东

相关推荐

葬爱~冷少:我当时都是上午刷力扣,下午背八股,有活给我先别急,没活就干自己的事情
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务