Home > database >  How do I put in the values (of different data types) from two HashMaps with the same keys into a new
How do I put in the values (of different data types) from two HashMaps with the same keys into a new

Time:12-18

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> PeopleAndNumbers = new HashMap<String, Integer>();

            HashMap<String, String> PeopleAndGroups = new HashMap<String, String>();

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

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

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

            HashMap<String, Integer> CombineMap = new HashMap<String, Integer>();

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

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

            System.out.println(CombineMap);
        }
    }

CodePudding user response:

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> combined = PeopleAndGroups.entrySet()
        .stream()
        .collect(Collectors.groupingBy(e -> e.getValue(),
                Collectors.summingInt(e -> PeopleAndNumbers.get(e.getKey()))));

Or you can iterate and merge entries into your destination map:

Map<String, Integer> combined = new HashMap<>();
PeopleAndGroups.forEach((k, v) ->
        combined.merge(v, PeopleAndNumbers.get(k), Integer::sum));

CodePudding user response:

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)
  •  Tags:  
  • java
  • Related