Tweet
Tweet

Reputation: 678

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 functions as they are HUGE !

Upvotes: 4

Views: 7076

Answers (6)

demongolem
demongolem

Reputation: 9708

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.

Upvotes: 0

Stephen C
Stephen C

Reputation: 718788

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

Upvotes: 1

fastcodejava
fastcodejava

Reputation: 41097

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()?

Upvotes: 0

卢声远 Shengyuan Lu
卢声远 Shengyuan Lu

Reputation: 32004

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

Upvotes: 3

user552961
user552961

Reputation: 140

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

Upvotes: 2

Gondim
Gondim

Reputation: 3048

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

Upvotes: 1

Related Questions