Home > Blockchain >  Hashmap inside a hashmap with arraylist
Hashmap inside a hashmap with arraylist

Time:06-17

Declaration:-

private static HashMap<String, HashMap<String, ArrayList>> parentMap = new HashMap<>(); private static HashMap<String, ArrayList> childMap = new HashMap<>();

How do I want to store data in hashmap?

"India":
        "EmployeeName":[A,B,C]
        
"China":
        "EmployeeName":[D,E,F]
    

Methods used:-

public static ArrayList<String> getMap(String parentkey, String childKey) {
    return parentMap.get(parentkey).get(childKey);
}


public static ArrayList<String> setMap(String parentkey, String childKey, String value) {
    childMap.computeIfAbsent(childKey, k -> new ArrayList<>()).add(value);
    parentMap.put(parentkey, childMap);
    return getMap(parentkey, childKey);
}

setMap("India", "EmployeeName", "A")
setMap("India", "EmployeeName", "B")
setMap("India", "EmployeeName", "C")
setMap("China", "EmployeeName", "D")
setMap("China", "EmployeeName", "E")
setMap("China", "EmployeeName", "F")

How data get stored and printed in hashmap while fetchng from getMap method:

System.out.println("India"   getMap("India").get("EmployeeName"));
System.out.println("China"   getMap("China").get("EmployeeName"));

"India" [A,B,C,D,E,F]   
"China" [A,B,C,D,E,F]
        

Whilst i know keeping the childKey name unique would do thejob for me but I wish to keep the same childKey name under each parentkey name and store the respecive value in arraylist.

Any solution to my problem is welcome.

CodePudding user response:

The problem is that you keep reusing the same childMap, regardless of which parentKey is being used. You need to look up the respective child map when adding values.

That means that childMap should be a local variable, nothing more. Delete your private static HashMap<String, ArrayList> childMap.

Try this:

public static ArrayList<String> setMap(String parentkey, String childKey, String value) {
  HashMap<String, ArrayList> childMap = parentMap.computeIfAbsent(parentkey, k->new HashMap<>());
  childMap.computeIfAbsent(childKey, k -> new ArrayList<>()).add(value);
  return getMap(parentkey, childKey);
}

Proof that this works

CodePudding user response:

Suggestion, don't have generic types and dont have static params

private HashMap<String, HashMap<String, ArrayList<String>>> parentMap = new HashMap<>(); 
private HashMap<String, ArrayList<String>> childMap = new HashMap<>();

Try to replace this method

public ArrayList<String> setMap(String parentkey, String childKey, String value) {
    childMap.putIfAbsent(childKey, new ArrayList<>()); // inserts a key only if the key is not already present
    childMap.get(childKey).add(value); // puts the value in the existing key and 
    
    if (!parentMap.containsKey(parentkey)) { // puts in the parent map only if not present. 
        parentMap.put(parentkey, childMap);
    }
}

Since the childmap is referenced already, No need to put again.

CodePudding user response:

If I was you I will do it in more "OOP way" so that you can benefit from static typing. Something like:

import java.util.List;
class Employee{
    String name;
    String getName(){
        return name;
    }
}


public class CompanyBranch{
    String national;
    List<Employee> employees;

    List<String> getEmployeeAllName(){
        return employees.stream().map(Employee::getName).toList();
    }
}
  • Related