题解 | 简单错误记录

简单错误记录

https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb?tpId=387&tqId=36843&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37

import java.util.*;
import java.util.Scanner;
import java.util.stream.Stream;


public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        LinkedHashMap<String, Integer> stringIntegerHashMap = new LinkedHashMap<String, Integer>(16,
                0.75f, false) {
            @Override
            protected boolean removeEldestEntry(Map.Entry eldest) {
                return size() > 100;
            }
        };
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.next();
            if (a == null || a.isEmpty()) {
                break;
            }
            a = a.substring(a.lastIndexOf("\\") + 1, a.length());
            if (a.length() > 16) {
                a = a.substring(a.length() - 16, a.length());
            }
            int b = in.nextInt();
            stringIntegerHashMap.put(a + " " + b,
                    stringIntegerHashMap.getOrDefault(a + " " + b, 0) + 1);
        }
        Stream<Map.Entry<String, Integer>> stream = stringIntegerHashMap.entrySet().stream();

        if (stringIntegerHashMap.size() > 8) {
            stream = stream.skip(Math.max(1,stringIntegerHashMap.size()-8));
        }
        stream.forEach(
                e -> System.out.println(e.getKey() + " " + e.getValue()));
    }
}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务