既然要put到HashMap里面去,为什么不在构造函数里put呢,这样的话第一次查找就可以从HashMap里读。
如果getscore的时候通过findInList找到对应的score,再put到HashMap里这样的话要第二次查找才能从Hashmap里读数据。
Students(List<Student> list) { this.list = list; cache = new HashMap<>(); for(Student stu:list) { cache.put(stu.name, stu.score); } } int getScore(String name) { // 先在Map中查找: Integer score = this.cache.get(name); if (score == null) { // TODO: } return score == null ? -1 : score.intValue(); }
cache应该是最好设计成缓存查过的数据,没有查过的不缓存,节省内存
或者 人家根本 就不来调用你的
getScore 方法
Sign in to make a reply
fredld22
既然要put到HashMap里面去,为什么不在构造函数里put呢,这样的话第一次查找就可以从HashMap里读。
如果getscore的时候通过findInList找到对应的score,再put到HashMap里这样的话要第二次查找才能从Hashmap里读数据。