题解 | #简单错误记录#

简单错误记录

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;
            }
            
        }
    }
}

全部评论

相关推荐

牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务