0
Map<Integer, Map<String, String>> movie = new HashMap<Integer,Map<String, String>>();
Map<String,String> sub = new HashMap<String,String>();

我这样做是为了填充并尝试迭代它:

Iterator<Element> releases = doc.select(
                    "div.pmovie > div.releases > div.release").iterator();
        Iterator<Element> subt = doc.select(
                "div.release-info > div.section > div.links > div > a")
                .iterator();
        int movindex = 1;
        while (subt.hasNext()) {
            Element rel = releases.next();
            Element subti = subt.next();
            release = rel.text();
            Log.v("RELEASES", release);
            subtitulo = subti.attr("href");
            Log.v("SUBTITULO", subtitulo);
            sub.put(release,subtitulo);
            movie.put(movindex, sub);
            movindex++;

        }

for (Entry<Integer, Map<String,String>>  entry : movie.entrySet()) {    
             key = entry.getKey();
             sub = entry.getValue();
                    for (Entry<String,String> entrada : sub.entrySet()) {

                        subtitle = entrada.getKey();
                        ruta = entrada.getValue();
               Log.v("SUB ITERATOR", key+" "+subtitle);


            }

我遇到的问题是内在的“for”。当我只需要外部 Map 键值引用的对 (K,V) 时,它正在获取所有“子”映射值。在 Map 中迭代此 Map 的正确方法是什么?如何使用外部地图的键从内部地图到达特定的(K,V)?

谢谢!

4

2 回答 2

0

您要达到的目标不是很清楚。但问题可能在于您填充地图的方式。您sub对所有键都使用相同的键,因此它们都指向包含所有记录的同一个映射。new HashMap<String, String>()每次递增时,您可能应该将 a 分配给 sub movieIndex

But if you do that, based on your code sample, each key (movieindex) will point to a map that contains only one entry.

于 2012-06-20T09:25:41.087 回答
0

First of See this one Java Hashmap: How to get key from value?

And then like this you can get that :

for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    List<String> list = entry.getValue();
    // Do things with the list
}

We can iterate over keys by calling map.keySet(), or iterate over the entries by calling map.entrySet().

于 2012-06-20T09:28:39.267 回答