题解 | #简单错误记录#
简单错误记录
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);
LinkedHashMap<String, Integer> errormap = new LinkedHashMap<>();
while (in.hasNextLine()) {
String ins = in.nextLine();
if (ins == null || ins.length() == 0) break;
String s[] = ins.split(" ");
String hanghao = s[1];
String[] paths = s[0].split("\\\\");
String name = paths[paths.length - 1];
if (name.length() > 16) name = name.substring(name.length() - 16);
String jilu = name + " " + hanghao;
if (errormap.containsKey(jilu)) {
int num = errormap.get(jilu);
errormap.replace(jilu, num + 1);
} else if (!errormap.containsKey(jilu)) {
errormap.put(jilu, 1);
}
}
//map获取特定的key比较麻烦,所以先把key都取出来放在arraylist中,然后再输出
ArrayList<String> outlist = new ArrayList<String>();
Iterator it = errormap.keySet().iterator();
while (it.hasNext()) {
String name = it.next().toString();
outlist.add(name + " " + errormap.get(name));
}
if (outlist.size() <= 8) {
for (String s : outlist) System.out.println(s);
} else {
for (int k = outlist.size() - 8; k < outlist.size(); k++) {
System.out.println(outlist.get(k));
}
}
}
}

