题解 | #简单错误记录#LinkedHashMap解决
简单错误记录
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);
// 注意 hasNext 和 hasNextLine 的区别
LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
while (in.hasNextLine()) { // 注意 while 处理多个 case
String str = in.nextLine();
if (str != null && str.length() != 0) {
String[] strs = str.split(" ");
String[] fileNames = strs[0].split("\\\\");
String fileName = fileNames[fileNames.length - 1];
if (fileName.length() > 16) {
fileName = fileName.substring(fileName.length() - 16, fileName.length());
}
fileName = fileName + " " + strs[1];
if (map.containsKey(fileName)) {
map.put(fileName, map.get(fileName) + 1);
} else {
map.put(fileName, 1);
}
}
}
List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
// Collections.reverse(list);
// for (int i = 0; i < 8; i++) {
// if (list.size() > i) {
// Map.Entry<String, Integer> entry = list.get(list.size() - i - 1);
// System.out.println(entry.getKey() + " " + entry.getValue());
// }
// }
list.stream().skip(Math.max(list.size() - 8,
0)).map(entry -> entry.getKey() + " " + entry.getValue()).forEach(
System.out::println);
}
}
#简单错误记录#
查看10道真题和解析