题解 | #简单错误记录#
简单错误记录
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<ErrorRecord,Integer> map = new LinkedHashMap<>(); while (in.hasNextLine()) { // 注意 while 处理多个 case String msg = in.nextLine(); ErrorRecord record = new ErrorRecord(); String[] arr = msg.split(" "); String name = arr[0].substring(arr[0].lastIndexOf("\\")+1); name = name.length()>16?name.substring(name.length()-16):name; record.setName(name); record.setRow(arr[1]); map.put(record,map.getOrDefault(record,0)+1); } Set<Map.Entry<ErrorRecord,Integer>> entrySet = map.entrySet(); Integer size = entrySet.size(); int count = 0; for(Map.Entry<ErrorRecord,Integer> entry:entrySet){ if(count<size-8){ count++; continue; } System.out.println(entry.getKey().name+" "+entry.getKey().row+" "+entry.getValue()); } } static class ErrorRecord { public String name;// 文件名(后16位) public String row;// 行号 public void setName(String name) { this.name = name; } public void setRow(String row) { this.row = row; } @Override public int hashCode() { return this.name.hashCode(); } @Override public boolean equals(Object e) { if(e instanceof ErrorRecord){ ErrorRecord r = (ErrorRecord)e; return this.name.equals(r.name) && this.row.equals(r.row); }else{ return false; } } } }