I think you are trying to do 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 parallel iterations over the elements in the HashMaps pair is dodgy. The only case where HashMaps entries will be “lined up” by keys is if the two HashMaps have the same keys with the same hash codes, and they are filled in the same order, starting with the recently allocated HashMaps.
So, I think 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); }
source share