开发者

How to Iterate Through Multiple Maps

So essentially, I have two hashmaps, one containing the following values:

rId33=image23
rId32=image22
rId37=image2

And the other containing this data:

{image2.jpeg=C:\Documents and Settings\image2.jpeg, image22.jpeg=C:\Documents and Settings\image22.jp开发者_如何学编程eg, image23.jpeg=C:\Documents and Settings\image23.jpeg}

I basically want to be able to iterate through the first map, find a match of the key's, if a match is found, get the associated value, then look in the second map, find a match in the keys, then pull out the associated value (meaning the file path).

I was thinking of doing something like this for example (the follow is simplified)...

String val2 = "rId33";

for (String rID: map.keySet())
{
     if (rID.contains(val2))
     {
         //enter code here
     }
}

I was looking at the methods available for something like .getValue or something, but I'm not entirely sure how to do that. Any help would be appreciated. Thanks in advance for any replies.

Edited Code with Help From Bozho

else if ("v:imagedata".equals(qName) && headingCount > 0)
{
    val2 = attributes.getValue("r:id");
    String rID = imageMap.get(val2);
    String path = imageLocation.get(rID + ".jpeg");

    for (String rels: imageMap.keySet())
    {
        if (rels.contains(val2))
        {
        inImage = true;
        image docImage = new image();

        imageCount++;

        docImage.setRelID(val2);
        docImage.setPath(path);
        addImage(docImage);
        }
    }


From what I see you don't need to iterate. Just:

String value1 = map1.get(key1);
if (value1 != null) {
    String path = map2.get(value1 + ".jpeg");
}

If you don't always know whether it's value1 + ".jpeg", but you just know that the key starts with the first value, then you can iterate the 2nd map with:

for (Map.Entry<String, String> entry : map2.entrySet()) {
    String key2 = entry.getKey();
    String value2 = entry.getValue();
    if (key.startsWith(value1)) {
        return value2;
    }
}

But note that the first code snippet is O(1) (both operations take constant time), while the 2nd is O(n)


And to answer the question as it is formulated in the title:

Get the iterators of both maps, and use it1.next() and it2.next() within a while loop. If any of the maps doesn't have more elements (it.hasNext()) - break.


That seems very inefficient. The entire point of a hash map is to do fast lookups. Do you really need to use that contains call on rID? In other words, can you change your hash map so that it directly contains the verbatim strings you want to search for and not just strings that contain the strings you want to search for as substrings? If yes, you could then use the answer given already. If not and if you must work with these data structures for whatever reason, the way to do what you're trying to do is something like:

String val2 = "rId33";
String path;

for (String rID: map.keySet())
{
    if (rID.contains(val2))
    {
        path = secondMap.get(map.get(rID)+".jpeg");
        break;
    }
}
if (path == null)
{
   //value not found
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜