English 中文(简体)
java hashmap 值
原标题:java hashmap values

我的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
}
问题回答

您需要从 shshmap 获取密钥 。 然后在密钥Set 上插入, 从 shmap 获取值 。

你的问题根本说不通

您不能拥有有 客户端名 hisritingforBook 的地图, 并在其中搜索 bookTitle 。 您需要选择其中一种方式 :

1) create method public double averageRating() inside Book class, and keep there, as field, rating being map with customerName to hisRatingOfBook

2) 使用你的方法:

public double averageRating(String booktitle) 
{ 
    numberOfRatingsPerCustomer/total
}

但 < 坚固> 使您的地图更复杂 < / 坚固 >, 将持有 < code > 客户 及其 < code> raduting (由书名+费率制成)

Here you go take these below code it will help you out to find your rating problem I have done to find average of Student marks in a class

import java.util.*;

public class QueQue {

public static float getAverage(HashMap<String, ArrayList<Integer>> hm, String name) {
    ArrayList<Integer> scores;
    scores = hm.get(name);
    if (scores == null) {
        System.out.println("NOT found");
    }

    int sum = 0;
    for (int x : scores) {
        sum += x;
    }
    return (float) sum / scores.size();
}
public static void main(String[] args) {
    HashMap<String, ArrayList<Integer>> hm = new HashMap<>();
    hm.put("Peter", new ArrayList<>());
    hm.get("Peter").add(10);
    hm.get("Peter").add(10);
    hm.get("Peter").add(10);

    hm.put("Nancy", new ArrayList<>());
    hm.get("Nancy").add(7);
    hm.get("Nancy").add(8);
    hm.get("Nancy").add(8);

    hm.put("Lily", new ArrayList<>());
    hm.get("Lily").add(9);
    hm.get("Lily").add(9);
    hm.get("Lily").add(8);

    System.out.println("Find the average of the Peter");
    float num = getAverage(hm, "Peter");

}
  }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签