计算Java中的dotproduct距离

时间:2012-05-24 11:23:43

标签: java arraylist hashmap dot-product

我需要做的是,计算两个客户的评级之间的点积距离。客户的评级记录在散列图中。

private HashMap<String,int[]> ratingmap;

hashmap中的键是客户名称,与之关联的是该客户的评级(他对书籍的评分)

我将如何做到这一点?

/**
 * calculate dot product distance between the ratings of two customers
 * @param name1 String name of customer 
 * @param name2 String name of customer 
 * @return int distance or ILLEGAL_INPUT if name1 or name2 are not customers
 */
public int distance(String name1, String name2) 
{
    return 0; //replace with code
}

以下是RatingsAnalysis

给出的另一部分细节
//collection of rated book titles
private BookList books;
//collection of customers and their ratings
private RatingsMap ratings;
//use a list of customer names from the ratings map for looping through the map
private ArrayList<String> customernames;

1 个答案:

答案 0 :(得分:0)

据推测,成员字段

private RatingsMap ratings;
RatingsAnalysis中的

Map名称=&gt;收视率。 distance方法中的任务是:

  1. 查看name1name2的评分,并将其存储在本地变量中(提示:使用get上定义的Map方法} interface)
  2. 对您在上面获得的这两个评级执行点积分析。您要么知道如何执行此操作,要么就此问题中的Rating提供有关其含义的更多信息。
  3. 返回结果
  4. 作为旁注,RatingsMap可以而且应该更强类型化:

    private RatingsMap<String, Rating> ratings;
    

    编辑:添加请求的骨架代码......

    public int distance(String name1, String name2) 
    {
        Rating r1 = this.ratings.get(name1);
        Rating r2 = this.ratings.get(name2);
    
        if(r1 != null && r2 != null) { 
           return ....
        }
    }