解题思路:
- 解题注意几个点:
- 如果获取URL中最后一个斜杠后面的单词
- int n = fileName.lastIndexOf("\\"); 或者最后一个斜杠的下标,然后进行截取。
- 根据题意得: 需要使用更有序的集合,又因为要存储 文件名和对应错误的次数, 所以选择linkedHashMap 这个结构;
- 如何使得map只输出最后八个错误记录呢:
- fileErrorMap.size() - count <= 8
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<String, Integer> fileErrorMap = new LinkedHashMap<>();
while (in.hasNextLine()) {
String str = in.nextLine();
String[] s = str.split(" ");
String fileName = s[0];
// 网址的String截取操作
int n = fileName.lastIndexOf("\\");
if (fileName.length() - n > 16)
fileName = fileName.substring(fileName.length() - 16);
else
fileName = fileName.substring(n + 1);
int lineNum = Integer.parseInt(s[1]);
// 次数
fileErrorMap.merge(fileName + "-" + lineNum, 1, Integer::sum);
}
int count = 0;
for (Map.Entry<String, Integer> it : fileErrorMap.entrySet()) {
// 这里的判断会使map只输出后8个
if (fileErrorMap.size() - count <= 8) {
String[] res = it.getKey().split("-");
System.out.println(res[0] + " " + res[1] + " " + it.getValue());
}
count++;
}
}
}