1

如果您能帮助我,我将不胜感激。

Map<String, Double> movieMap = new HashMap<String, Double>;
movieMap.add("MovieA", 3);
movieMap.add("MovieB", 5);
movieMap.add("MovieC", 4);
movieMap.add("MovieD", 3.5);
movieMap.add("MovieE", 2.5);

Map<String, Double> movieMap2 = new HashMap<String, Double>;
movieMap2.add("MovieA", 3.5);
movieMap2.add("MovieB", 2.5);
movieMap2.add("MovieC", 4.5);
movieMap2.add("MovieD", 2);
            .
            .
            .
Map<String, Double> movieMapi = new HashMap<String, Double>;
movieMap2.add("MovieA", 5);
movieMap2.add("MovieC", 1.5);
movieMap2.add("MovieD", 2);

Map<String, Map<String, Double>> userMap = new HashMap<String, Map<String, Double>>;
userMap.add("Nick", movieMap);
userMap.add("Bill", movieMap2);
           .
           .
           .
userMap.add("Tom", movieMapi);

我想要做的是做一个函数比较(userA,userB)并将常见的电影及其费率放入一个新的地图中。例如, compare(Nick,Tom) 的输出应该是带有键的 Map - 值:{{"MovieA", {5,3}}, {"MovieC", {4,1.5}}, {"MovieD ", {3.5,2}}}

我似乎很难解决它..你能帮帮我吗?
谢谢...

4

2 回答 2

3

您可以使用Map.keySet()方法遍历地图中的键:

Set<String> nickKeys = userMap.get("Nick").keySet();
Set<String> tomKeys = userMap.get("Tom").keySet();

您的代码可能如下所示:

Map<String, Double> nickMap = userMap.get("Nick");
Map<String, Double> tomMap = userMap.get("Tom");

Map<String, List<Double>> commonMap = new HashMap<String, List<Double>>();

for (String movieName : nickMap.keySet()) {
    if (tomMap.containsKey(movieName)) {
        List<Double> tempList = new ArrayList<Double>();
        tempList.add(nickMap.get(movieName));
        tempList.add(tomMap.get(movieName));
        commonMap.put(movieName, tempList);
    }
}
于 2013-01-29T21:24:19.870 回答
2

这应该适合您的需求:

public static Map<String, List<Double>> compare(Map<String, Map<String, Double>> userMap, String user1, String user2) {
    // get both users movies and rates
    Map<String, Double> user1Map = userMap.get(user1);
    Map<String, Double> user2Map = userMap.get(user2);
    // if at least one of the both users doesn't exists in the initial map
    if (null == user1Map || null == user2Map) {
        // exit
        throw new IllegalArgumentException();
    }
    // will contains the common movies and the respective rates to return
    Map<String, List<Double>> commonMovies = new HashMap<String, List<Double>>();
    // for each movie/rate of the first user
    for (Entry<String, Double> user1Entry : user1Map.entrySet()) {
        // try to find the same movie in the second user's map
        Double user2Value = user2Map.get(user1Entry.getKey());
        // if both contains the same movie
        if (null != user2Value) {
            // create a new list
            List<Double> rates = new LinkedList<Double>();
            // list.get(0) will always be the rate of the first user passed as a parameter to this method
            rates.add(user1Entry.getValue());
            // and list.get(1) the rate of the second one
            rates.add(user2Value);
            commonMovies.put(user1Entry.getKey(), rates);
        }
    }
    return commonMovies;
}

致电:

Map<String, List<Double>> commonMovies = compare(userMap, "Nick", "Tom");

请注意,我们通常要求 OP 在给出解决方案之前提供一些他已经尝试过的代码片段,但我认为迭代地图(和嵌套地图)对于初学者来说非常困难,并且单个注释的解决方案有时比几个个人尝试更好;)

于 2013-01-29T21:29:17.127 回答