Two HashMap Iterations

I have two HashMaps and I can iterate over both hash files with the 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 repeat two hashmaps simultaneously without using two loops?

I currently have a method that accepts two parameters, and each parameter value is stored in the first and second hash cards. I have to repeat the first hash and then the second to get the values. I think there should be a good way to do this, but I don't know :(

PS: there may be some errors in the code above, as this is just an example to explain my problem. Each iterator is a method in the original program and takes one parameter. I could not copy past functions in real time, as they are HUGE!

+4
source share
6 answers

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

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

Put the values ​​from 2 cards in the 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)); } 
+3
source
  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; } } 
+1
source

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); } 
+1
source

You cannot iterate over two cards in one cycle unless they have the same set of keys. I don’t know how you find firstVal and SecondVal , it seems a bit ambiguous. Maybe you can get these two values ​​on Map.get() ?

0
source

If the Map objects themselves are parallel, then perhaps the best solution would be to create a custom class instead of using a Map. You can guarantee the iteration order in Maps, as already mentioned in some util objects (the other is LinkedHashMap). This does not give a developer license, although using objects as if they were arrays or lists.

0
source

Source: https://habr.com/ru/post/1333379/


All Articles