0

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.

1
  • 1
    in your setMap method, what is childMap? Why does it already exists (and even static)? You need to use childmap = parentMap.computeIfAbsent(...);. Otherwise you use the same childMap everywhere which explains your output. Commented Jun 17, 2022 at 8:44

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

why is there no call to parentMap.get in case there's already a childmap for it? And why should childMap be a field and not a local variable?
@f1sh, there is no need to use get method, since childmap is a global variable and parent map has the reference to the global variable -> changing childMap is reflected directly in parentMap
0

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();
    }
}

4 Comments

This does not solve any of the problems that OP has.
May be I'm new to answer SO question but what I've given him is fishing rod not fish. And the problem I see here is that he is coding Java in "Python ways". Maybe I'm misunderstanding...
yes, an answer should be the fish, cooked and deboned. Comments are good for leading the OP to their own answer.
@matt Oh I didn't know that. I'll learn from this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.