读懂题目是关键。首先分离错误的行号和文件名,再把文件的后缀名截取下来(最后一个斜杠后的内容)并且超过16个字符的就取最后16个字符。一个错误记录由文件名和行号组成,把这个作为key放入map中即可。最后只需打印不超过8个记录即可。
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<String,Integer> map = new LinkedHashMap<>();
while (in.hasNext()) {
String str = in.nextLine();
String[] strs = str.split(" ");
int row = Integer.parseInt(strs[1]);
String file[] = strs[0].split("\\\\");
String fileName = file[file.length-1];
String saveFileName = fileName.length()>16?fileName.substring(fileName.length()-16)+" "+row:fileName+" "+row;
if(map.get(saveFileName)==null)
map.put(saveFileName,1);
else
map.put(saveFileName,map.get(saveFileName)+1);
}
String res[] = new String[map.size()];
int index = 0;
for(Map.Entry<String,Integer> entry:map.entrySet()){
if(map.size() - index <= 8)
System.out.println(entry.getKey()+" "+entry.getValue());
index++;
}
}
}