不用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);
//定义数据结构
Map<String,Integer> countMap = new HashMap<>();
LinkedList<String> queue = new LinkedList<>();
while (in.hasNextLine()){
String s = in.nextLine();
//处理字符串
String[] arr1 = s.split("\\\\");
String[] arr2 = arr1[arr1.length - 1].split(" ");
String fileName = arr2[0];
String lineNum = arr2[1];
while(fileName.length()>16){
fileName = fileName.substring(1);
}
String key = fileName+" "+lineNum;
//只容纳八个
if(!countMap.containsKey(key)){
queue.addLast(key);
if(queue.size()>8){
queue.removeFirst();
}
}
//统计数量
if(countMap.containsKey(key)){
countMap.replace(key,countMap.get(key)+1);
}else {
countMap.put(key,1);
}
}
for (String item : queue) {
System.out.println(item+" "+countMap.get(item));
}
}
}
查看30道真题和解析