繁体   English   中英

HashMap中的HashMap

[英]HashMap in a HashMap

更新:-

问题陈述是-

我需要存储这些东西-

For ID corresponding to 1, I need to Store these things

 - Key CGUID(String CGUID) and its Value, Key SGUID(String SGUID) and its Value, Key PGUID(String PGUID) and its Value, Key UID(String UID) and its Value, Key ALOC(String ALOC) and its Value

For ID corresponding to 2, I need to Store these things

- Key CGUID(String CGUID) and its Value, Key SGUID(String SGUID) and its Value, Key PGUID(String PGUID) and its Value, Key UID(String UID) and its Value, Key ALOC(String ALOC) and its Value

For ID corresponding to 3, I need to Store these things

- Key CGUID(String CGUID) and its Value, Key SGUID(String SGUID) and its Value, Key PGUID(String PGUID) and its Value, Key UID(String UID) and its Value, Key ALOC(String ALOC) and its Value

因此,对于这个问题,我正在考虑制作这样的数据结构-

public static LinkedHashMap<String, Integer> GUID_VALUES = new LinkedHashMap<String, Integer>();

public static LinkedHashMap<Integer, LinkedHashMap<String, Integer>> GUID_ID_MAPPING = new LinkedHashMap<Integer, LinkedHashMap<String, Integer>>();

有没有比这更好的方法了?

最终要存储什么尚不完全清楚。

如果您只想要一张地图:

LinkedHashMap<Integer, LinkedHashMap<String, Integer>>

但是“ etc etc etc”到底是什么呢? 每个家长ID是否有多个“孩子” ID? 如果是这样,那么您可能想要某种树(可以通过地图实现,但对其进行抽象似乎是合理的。)

在Java中,您可以使用以下方法实现此目的:

public class Data {
    public static LinkedHashMap<String, Integer> GUID_VALUES = new LinkedHashMap<String, Integer>();
    public static LinkedHashMap<Integer, Map<String, Integer>> GUID_ID_MAPPING = new LinkedHashMap<Integer, Map<String, Integer>>();

    static {
        Integer someNumber = 0; //or another value, its for initialize the key
        GUID_ID_MAPPING.put(someNumber,GUID_VALUES);
    }
}

不过,我仍然不明白为什么您真的需要这个。 我建议您发布功能要求,并尝试选择更好的设计来解决问题。

您可能想要一个多键映射,如Apache Commons Collections中可用的映射:

http://commons.apache.org/collections/api-release/org/apache/commons/collections/map/MultiKeyMap.html

暂无
暂无

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

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