【发布时间】:2012-05-24 04:22:25
【问题描述】:
我的 hashmap 包含一个键,它是客户的名字,值是所有书籍的评分。我必须计算给定书名的平均评分。
如何访问哈希图中的所有值(评级)?有没有办法做到这一点?
这是我的一段代码:
/**
* calculate the average rating by all customers for a named book
* only count positive or negative ratings, not 0 (unrated)
* if the booktitle is not found or their are no ratings then
* return NO_AVERAGE
* @param booktitle String to be rated
* @return double the average of all ratings for this book
*/
public double averageRating(String booktitle)
{
numberOfRatingsPerCustomer/total
}
【问题讨论】:
-
哈希映射将键映射到单个值。你能澄清一下你的数据结构是什么吗?
-
所有 Java
Maps(包括 HashMaps)都有一个 method namedvalues(),它返回一个值的集合。 -
好的,我正在尝试创建一个包含所有书单的数据结构,然后客户对他/她名下的书进行评分。你能告诉我如何计算平均评分
-
@AkanshaDahiya:平均评分=总评分/评分数。
-
我不知道如何获得评分数,它不会只是 hashmap 的大小