/*
* 请编写一个根据name查找score的程序,并利用Map充当缓存,以提高查找效率:
*/
int getScore(String name) {
// 先在Map中查找:
Integer score = this.cache.get(name);
if (score == null) {
// Map中未找到在List中查找
score = this.findInList(name);
if(score ==null) {
// List中未找到,返回-1
return -1;
}else {
// List中找到,存入map 缓存cache
cache.put(name, findInList(name));
}
return score == null ? -1 : score.intValue();
Sign in to make a reply
王羲之666666
/*
* 请编写一个根据name查找score的程序,并利用Map充当缓存,以提高查找效率:
*/
int getScore(String name) {
// 先在Map中查找:
Integer score = this.cache.get(name);
if (score == null) {
// Map中未找到在List中查找
score = this.findInList(name);
if(score ==null) {
// List中未找到,返回-1
return -1;
}else {
// List中找到,存入map 缓存cache
cache.put(name, findInList(name));
}
}
return score == null ? -1 : score.intValue();
}