开发者

two HashMap iteration

I have two HashMaps and I can iterate both hashmaps with following code

Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry)it.next();
    String firstVal = pairs.getValue();
}

Iterator it2 = mp2.entrySet().iterator();
while (it2.hasNext()) {
    Map.Entry pairs2 = (Map.Entry)it.next();
    String SecondVal = pairs2.getValue();
}

myFunction(firstVal, SecondVal)

Is there anyway to iterate two hashmaps at the same time without using two loops?

Currently, I have a method that accepts two parameters and each parameter value is stored in first and second hashmap. I have to iterate first hash then second to get values. I think there must be a good way to do it but I don't know :(

P.S: there could be some errors in above code as this is just an example to explain my problem. Each iterator is a method in original program and accept one parameter. I couldn't copy past real time functi开发者_StackOverflow社区ons as they are HUGE !


Put values of 2 maps into a list, then loop the list:

//Merge 2 values of maps to a list
List<String> mergedList = new ArrayList<String>();
mergedList.addAll(map1.values());
mergedList.addAll(map2.values());

int size = map1.size() < map2.size() ? map1.size() : map2.size();
for(int i=0; i < size; i++){
    myFunction(mergedList.get(i), mergedList.get(map1.size() + i));
}


your code looks good. Just use while loop as inner and outter loops to iterate on both HashMaps. In second while loop, you can call your function to perfrom whatever you want.

while loop (iterate first hashmap)
    second while loop(iterate second hashmap)
         call your function here and pass values


    Map.Entry pairs;
    String firstValue = null;
String secondValue = null;
    while(it.hasNext() || it2.hasNext()){
     if (it.hasNext()){
      pairs = (Map.Entry)it.next();
      firstValue = pairs.getValue();
     }
     if (it2.hasNext(){
      pairs = (Map.Entry)it2.next();
      secondValue = pairs.getValue();
     }
     if (firstValue != null && secondValue != null){
       yourMethodHere();
       firstValue = null;
       secondValue = null;
     }
    }


I think what you are trying to do is this:

if (mp.size() != mp2.size()) {
    throw SomeException("mismatched parameters");
}
Iterator it = mp.entrySet().iterator();
Iterator it2 = mp2.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry)it.next();
    String firstVal = pairs.getValue();
    Map.Entry pairs2 = (Map.Entry)it.next();
    String secondVal = pairs2.getValue();
    myFunction(firstVal, secondVal);
}

Note that doing a parallel iteration over the entries in a pair of a HashMaps is dodgy. The only case where the entries of the HashMaps will "line up" by keys is if the two HashMaps have identical keys with the same hashcodes, and they were populated in the same order starting from newly allocated HashMaps.

So therefore, I think that you really need to do something like this.

if (mp.size() != mp2.size()) {
    throw SomeException("mismatched parameters");
}
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry)it.next();
    String firstVal = pairs.getValue();
    String SecondVal = mp2.get(pairs.getKey());
    myFunction(firstVal, SecondVal);
}


You cannot iterate two maps in one loop unless they have same set of keys. I don't know how you find firstVal and SecondVal, it seems little ambiguous. Maybe you could get the those two values by Map.get()?


If the Map objects are themselves parallel, then perhaps the better solution would be to create a custom Class instead of using a Map. One can guarantee the iteration order in Maps as has already been mentioned in certain util Objects (another one is LinkedHashMap). This does not give the developer license though to use the objects as if they were arrays or lists.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜