import java.util.Scanner; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Map<ErrorRecord, Integer> map = new LinkedHashMap<>(); while (in.hasNextLine()) { // 重写 ErrorRecord 的 hashcode 和 equals 方法 在map中已经存在了错误记录 则记录数+1 map.compute(new ErrorRecord(in.nextLine()), (k, v) -> v == null ? 1 : ++v); } solution(map); } public static void solution(Map<ErrorRecord, Integer> map) { int cur = 0; int size = map.size(); for (Map.Entry<ErrorRecord, Integer> entry : map.entrySet()) { // 打印最后8个 if (size - cur <= 8) { System.out.println(entry.getKey() + " " + entry.getValue()); } cur++; } } private static class ErrorRecord { private String name; private int line; public ErrorRecord(String s) { String[] temp = s.split(" "); String fileName = temp[0].substring(temp[0].lastIndexOf("\\") + 1); this.name = fileName.length() > 16 ? fileName.substring( fileName.length() - 16) : fileName; this.line = Integer.parseInt(temp[1]); } @Override public boolean equals(Object er) { return name.equals(((ErrorRecord) er).name) && line == ((ErrorRecord) er).line; } @Override public int hashCode() { return (this.name + " " + this.line).hashCode(); } @Override public String toString() { return this.name + " " + this.line; } } }