LinkedHashMap的简单使用,只需要输出最后面的8条记录就好:
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Map<String, Integer> map = new LinkedHashMap<>();
while(sc.hasNextLine()){
String str = sc.nextLine();
String sub = str.substring(Math.max(str.lastIndexOf("\\") + 1, str.lastIndexOf(" ") - 16));
Integer count = map.get(sub);
if(count == null){
map.put(sub, 1);
}else{
map.put(sub, count + 1);
}
}
int index = 0;
for(Map.Entry<String, Integer> e : map.entrySet()){
if(map.size() - index <= 8){
System.out.println(e.getKey() + " " + e.getValue());
}
++ index;
}
}
}