循环遍历地图中嵌套地图的值

时间:2013-01-29 21:19:18

标签: java

如果你能帮助我,我将非常感激。

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);

我想要做的是创建一个函数 compare(userA,userB)并将公共电影及其费率放入新的地图中。举例来说,比较的输出(尼克,汤姆)应该是带键的地图 - 值: {{“MovieA”,{5,3}},{“MovieC”,{4,1.5}}, {“MovieD”,{3.5,2}}}

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

2 个答案:

答案 0 :(得分:2)

您可以使用 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);
    }
}

答案 1 :(得分: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提供他在提供解决方案之前已经尝试过的一些代码片段,但我相信对于初学者来说迭代地图(和嵌套地图)是非常困难的,并且单个评论的解决方案是有时比几次个人尝试;)

更好