Title: How do I put in the values (of different data types) from two HashMaps with the same keys into a new third HashMap?
Tags: java
Question: I need to make a third HashMap based off the values from the PeopleAndNumbers and PeopleAndGroups hashmaps. But the third HashMap would only have the 3 groups as keys and the total amounts from the people in that group as values.
(Also worth noting that the keys in the first both maps are the same.)
Here are the contents of the first two maps:
PeopleAndNumbers: ```{p1=1, p2=3, p3=2, p4=3, p5=1, p6=2} ```
PeopleAndGroups: ```{p1=GroupA, p2=GroupB, p3=GroupC, p4=GroupB, p5=GroupC, p6=GroupA}```
I need to make a third HashMap that'd print out like this:
CombineMap: ```{GroupA=3, GroupB=6, GroupC=3}```
Here is what the code looks like so far:
```import java.util.HashMap;

public class HashmapTest {

        public static void main(String[] args) {
            
            HashMap<String, Integer&gt; PeopleAndNumbers = new HashMap<String, Integer&gt;();

            HashMap<String, String&gt; PeopleAndGroups = new HashMap<String, String&gt;();

            PeopleAndNumbers.put(&quot;p1&quot;, 1);
            PeopleAndNumbers.put(&quot;p2&quot;, 3);
            PeopleAndNumbers.put(&quot;p3&quot;, 2);
            PeopleAndNumbers.put(&quot;p4&quot;, 3);
            PeopleAndNumbers.put(&quot;p5&quot;, 1);
            PeopleAndNumbers.put(&quot;p6&quot;, 2);

            PeopleAndGroups.put(&quot;p1&quot;,&quot;GroupA&quot;);
            PeopleAndGroups.put(&quot;p2&quot;,&quot;GroupB&quot;);
            PeopleAndGroups.put(&quot;p3&quot;,&quot;GroupC&quot;);
            PeopleAndGroups.put(&quot;p4&quot;,&quot;GroupB&quot;);
            PeopleAndGroups.put(&quot;p5&quot;,&quot;GroupC&quot;);
            PeopleAndGroups.put(&quot;p6&quot;,&quot;GroupA&quot;);

            System.out.println(PeopleAndNumbers);
            System.out.println(PeopleAndGroups);

            HashMap<String, Integer&gt; CombineMap = new HashMap<String, Integer&gt;();

            //Insert method to do this here, How would I go about this?

            System.out.println(&quot;Expected Output for CombineMap should be&quot;);
            System.out.println(&quot;{GroupA=3, GroupB=6, GroupC=3}&quot;);

            System.out.println(CombineMap);
        }
    }
```

Comment: @ shmosel, it was an error, I meant for it to be GroupC=3 (I edited the post to fit this now) 
@ Alexey Ah alright, I'll try to see how that goes
@ Louis Ah alright, noted

Comment: Welcome to Stack Overflow.
Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question.
Please show your attempts you have tried and the problems/error messages you get from your attempts.

Comment: Note that the Java convention is for variables like `PeopleAndNumbers` to start with a lowercase letter, e.g. `peopleAndNumbers`.

Comment: Oh well, you need to iterate one of the maps, find the corresponding value in the other map, construct a new String value and insert it into the third map. A separate question is what to do when the key sets don't match in the two input maps.

Comment: How do you get `GroupC=4`?


Here is another answer: To achieve that you need  to iterate over the entries of the PeopleAndGroups map and do the following for each entry:

check if the combinedMap has a key equal to the value of the current entry

If the key doesn't exist put the key with value 0: ```combinedMap.put(entry.getValue(), 0)```


Get the value of the entry's key from the PeopleAndNumbers and let's call it N: ```int N = PeopleAndNumbers.get(entry.getKey())```
add N to the old value of your result map:
```combinedMap.put(entry.getValue(), combinedMap.get(entry.getValue()) + N)```



Here is another answer: If I understand you correctly, you want to sum Numbers by Group, using the common keys to join them. If so, you can do it pretty easily with streams:
```Map<String, Integer&gt; combined = PeopleAndGroups.entrySet()
        .stream()
        .collect(Collectors.groupingBy(e -&gt; e.getValue(),
                Collectors.summingInt(e -&gt; PeopleAndNumbers.get(e.getKey()))));
```
Or you can iterate and merge entries into your destination map:
```Map<String, Integer&gt; combined = new HashMap<&gt;();
PeopleAndGroups.forEach((k, v) -&gt;
        combined.merge(v, PeopleAndNumbers.get(k), Integer181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16sum));
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:12/17/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java>', 'Title': 'How do I put in the values (of different data types) from two HashMaps with the same keys into a new third HashMap?', 'ViewCount': 93, 'id': 70399592, 'user_AboutMe': None, 'user_AccountId': 23123646.0, 'user_DisplayName': 'ATR', 'user_Reputation': 65.0, 'user_Views': 10.0, 'user_id': 17229808.0}}