0

I have created two hashmaps and I want to iterate both of them inside the same for loop.

HashMap<String,Double> hashmapA = new HashMap<>();
HashMap<String,Double> hashmapB = new HashMap<>();

So, if I iterate the elements in hashmapA as follows:

for(String map1:hashmapA.keyset()){
...
}

How can I iterate the values of hashmapB inside the same loop? Actually, I don't want to use the inner loop.

3
  • Do they have the same keys? Commented Mar 5, 2020 at 21:32
  • Some of the keys in hashmapB are the same as hashmapA Commented Mar 5, 2020 at 21:32
  • is the question how to iterator two hashmaps in one loop ? @Hami Commented Mar 5, 2020 at 21:54

2 Answers 2

1

If you just want to iterate over all keys:

Just create a new HashSet out of the keys of the first Map and add the keys of the second Map:

Collection<Map.Entry<String,Double>> entries=new HashSet<>(hashmapA.entrySet());
keys.addAll(hashmapB.entrySet());
for(Map.Entry<String,Double> entry:entries){
    String key=entry.getKey();
    Double value=entry.getValue();
    ...
}

This can also be done using Java 8 Streams:

for(Map.Entry<String,Double> entry: Stream.concat(hashmapA.entrySet().stream(),hashmapB.entrySet().stream()){
     String key=entry.getKey();
    Double value=entry.getValue();
    ...
}

If you just want the intersection of the maps, you can use:

Collection<String> keys=new HashSet<>(hashmapA.keySet());
keys.retainAll(hashmapB.keySet());
for(String key:keys){
    Double aValue=hashmapA.get(key);
    Double bValue=hashmapB.get(key);
    ...
}

Or (with Streams):

for(String key: hashmapA.entrySet().stream().filter(k->hashmapB.containsKey(k))){
    Double aValue=hashmapA.get(key);
    Double bValue=hashmapB.get(key);
    ...
}

As @bsaverino stated in the comments:

Regarding your latest remark @Hami then just iterating over the keys of hashmapA and using hashmapB.containsKey(...) could have been enough.

The following will also work in your case:

for(String key:hashmapA.keySet()){
    if(hashmapB.containsKey(key){
        Double aVal=hashmapA.get(key);
        Double bVal=hashmapB.get(key);
    }
}
Sign up to request clarification or add additional context in comments.

14 Comments

I Fixed that :)
You can get the values from both sets using retainAll(you'll need to use .keySet() for this)
I've added this too.
Regarding your latest remark @Hami then just iterating over the keys of hashmapA and using hashmapB.containsKey(...) could have been enough.
You came up with the idea(even if I'm sure that many peaple already came up with the same idea) - You deserve the credit.
|
1

Iterator is one of the best choice either with keyset or entrySet

Iterator hmIterator1 = hashmapA.entrySet().iterator(); 
Iterator hmIterator2 = hashmapB.entrySet().iterator(); 

 while (hmIterator1.hasNext() && hmIterator2.hasNext()) { 

       hmIterator1.next();
       hmIterator2.next();

    } 

2 Comments

A necessary improvement I guess is to put an OR (||) instead of AND (&&) in the while condition and then check inside loop if each iterator .hasNext() is true before calling .next(). And by the way performing some pre-processing on the hashmaps before some iterations would sound better I believe (i.e. perform union and exclude duplicates or intersects).
I would leave that to OP either to use OR and AND condition with the logic inside of loop @bsaverino

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.