Discuss / Java / 练习题有点疑惑

练习题有点疑惑

Topic source

fredld22

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

既然要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应该是最好设计成缓存查过的数据,没有查过的不缓存,节省内存

cache应该是最好设计成缓存查过的数据,没有查过的不缓存,节省内存

或者  人家根本 就不来调用你的 

getScore 方法


  • 1

Reply