简体   繁体   English

Hashmap 在 hashmap 和 arraylist 里面

[英]Hashmap inside a hashmap with arraylist

Declaration:-宣言:-

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

How do I want to store data in hashmap? 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:从 getMap 方法获取数据时如何在 hashmap 中存储和打印数据:

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.虽然我知道保持 childKey 名称唯一会为我完成这项工作,但我希望在每个 parentkey 名称下保留相同的 childKey 名称并将相应的值存储在 arraylist 中。

Any solution to my problem is welcome.欢迎解决我的问题。

The problem is that you keep reusing the same childMap , regardless of which parentKey is being used.问题是,无论使用哪个parentKey ,您都会重复使用同一个childMap You need to look up the respective child map when adding values.添加值时,您需要查找相应的孩子 map。

That means that childMap should be a local variable, nothing more.这意味着childMap应该是一个局部变量,仅此而已。 Delete your private static HashMap<String, ArrayList> childMap .删除您的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证明这有效

Suggestion, don't have generic types and dont have static params建议,不要有通用类型,也不要有 static 参数

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.由于已经引用了childmap,所以不用再放了。

If I was you I will do it in more "OOP way" so that you can benefit from static typing.如果我是你,我会以更“面向对象的方式”来做,这样你就可以从 static 打字中受益。 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM