Discuss / Java / 参考一下我的想法,看看不会好理解一些

参考一下我的想法,看看不会好理解一些

Topic source

TraZ_

#1 Created at ... [Delete] [Delete and Lock User]

首先可以把Students理解成一个班级,然后,它持有学生列表和学生成绩缓存。

有个人拿个一个name来询问成绩。

先看他是不是第一次来询问。

Integer score = this.cache.get(name); //null则表示第一次来问,not null则表示不是

Y (null)->则在全班人中挨个询问,得到的结果是null,则让这个问成绩的人滚蛋,因为他不是你班的学生,不需要把他的成绩null值存入到缓存中。结果为not null,则把查出来的成绩先存缓存,再把这个成绩赋值给score,返回出去。

N(not null)->表面这个人不是第一次来查询成绩,只需要从缓存中拿出这个成绩给这个人即可。

--TODO

Integer cacheScore = findInList(name);
if (cacheScore != null) {
    cache.put(name,findInList(name));    
    score = cacheScore;
}

张无疗

#2 Created at ... [Delete] [Delete and Lock User]
cache.put(name,findInList(name));  

改成

cache.put(name,cacheScore);

再做一次查找会浪费时间

  • 1

Reply