Home > Enterprise >  Go through multiple hashmaps of different sizes to extract values with common keys
Go through multiple hashmaps of different sizes to extract values with common keys

Time:08-04

So I essentially want to go through all the elements in the arraylist and match it with the keys of each hashmap and for the values of the common keys I want to make a new arraylist.

Essentially if keygrades is on value 1, I want to check every hashmap with the key 1 and then extract all the values associated with that key and make a brand new arraylist with those values.

ArrayList <String> keygrades = new ArrayList<>();
HashMap <String,String> gradeA = new HashMap<>();
HashMap <String,String> gradeB = new HashMap<>();
HashMap <String,String> gradeC = new HashMap<>();
HashMap <String,String> gradeD = new HashMap<>();

This is what is in the hashmap:

keygrades = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
gradeA = {11=134, 1=100, 3=110, 4=120, 15=142, 5=130}
gradeB = {2=102, 3=103, 6=108, 8=109}
gradeC = {3=104, 5=105, 6=111}
gradeD = {3=122, 4=123}

For example for key 1 I want a new arraylist which would be (100,"","","") ""= empty string. For key 2 I want a new arraylist which would be ("",102,"","").It would continue going through hashmaps in order and inputting into new arraylist each time.

CodePudding user response:

I recommend writing a method with this struture:

  1. Create an ArrayList<ArrayList>, which will hold the outcoming ArrayLists containing the different values for common keys.

  2. Iterate over Arraylist containing the keys.

  3. Create an ArrayList that will get the 4 values.

  4. Check all 4 Hashmaps with the current key using HashMap.get(key). If the outcome is null, you should add "" to your ArrayList, otherwise you enter the value to the ArrayList inside the loop.

After it iterated through the ArrayList containing the keys. You should have an ArrayList<ArrayList> holding exactly as much ArrayList containing the values for similar keys, as you ArrayList of keys size.

CodePudding user response:

You can achieve this by transforming your maps into a stream of maps and extracting the values for a particular key.

List<String> values = Stream.of(gradeA,
                                gradeB,
                                gradeC,
                                gradeD)
                            .map(map -> map.getOrDefault("1", ""))
                            .collect(Collectors.toList());

To reuse this, you'll have to create a function that handles the combining of the maps and a function that extracts values from that stream of maps.

The example should get you on your way.

CodePudding user response:

You might need to create a method like this:

public ArrayList<String> createArryList(ArrayList <String> keyGrades, HashMap <String,String> grade)
    {
        ArrayList <String> ars = new ArrayList<>();
        for(String keyGrade: keyGrades)
        {
            ars.add(keyGrade);
            var str = grade.get(keyGrade);
            ars.set(Integer.parseInt(keyGrade)-1, str != null? str:"emptyString");
        }

        return ars;
    }

You first need to initialize the arrayList by adding the keyGuard and then if the grade has a value for the specified keyGrade it would be stored in the str variable and in your main method you can do it like this for example:

public static void main(String[] args)
    {
        ArrayList<String> keygrades = new ArrayList<>();
        HashMap<String,String> gradeA = new HashMap<>();
        keygrades.add("1");
        keygrades.add("2");
        keygrades.add("3");
        keygrades.add("4");
        gradeA.put("1", "100");
        gradeA.put("11", "102");
        gradeA.put("3", "104");
        gradeA.put("4", "122");
        gradeA.put("5","123");
        var arr = createArryList(keygrades,gradeA);
        for(String s: arr)
        {
            System.out.print(s  " ");
        }
    }

The output would be like this:

100 emptyString 104 122 
  • Related