Java HashMap中的HashMap

Java HashMap中的HashMap,java,hashmap,linkedhashmap,Java,Hashmap,Linkedhashmap,更新:- 问题陈述是- 我需要储存这些东西- 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

更新:-

问题陈述是-

我需要储存这些东西-

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>>();
public静态LinkedHashMap GUID_value=new LinkedHashMap();
公共静态LinkedHashMap GUID_ID_MAPPING=new LinkedHashMap();

还有比这更好的方法吗?

您可能需要一个多键映射,就像Apache Commons集合中提供的那样:


还不完全清楚您最终要存储什么

如果您只需要一张地图:

LinkedHashMap<Integer, LinkedHashMap<String, Integer>>
LinkedHashMap

但最后的“等等”是什么?每个父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);
    }
}
公共类数据{
公共静态LinkedHashMap GUID_值=新LinkedHashMap();
公共静态LinkedHashMap GUID_ID_MAPPING=new LinkedHashMap();
静止的{
Integer someNumber=0;//或其他值,用于初始化键
GUID\u ID\u MAPPING.put(someNumber,GUID\u值);
}
}

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

泛型用于类型,而不是值。听起来像是一个支持基于路径的查询的树,一个la XPath。CGUID键和值是否与ID 1和ID 2相关,换句话说,它将是相同的映射或结构(这也适用于其他键/值对)?通过CGUID键,我指的是字符串CGUID,我用更多信息更新了问题。再次阅读注释,我对其进行了编辑。嗯,不确定--问题中不清楚,但它看起来更像一个键,带有嵌套的映射--更像一棵树。
LinkedHashMap
在左手边side@assylias更可能在右侧作为一个ctor,在左侧为h另一方面,你可能想抽象地图实现。实际上,lhs上的
map
,以及我在rhs上所说的,对吗?;-)@assylias-Nope,在左边,我几乎总是使用
map>
,除非我特别需要传达它,保留插入顺序。我刚刚更新了我的问题,让我知道它是否仍然令人困惑。我经常使用地图的地图(虽然通常是抽象的),这是一个非常典型的结构,不是吗?@DaveNewton是的,我只是想检查问题是否比这更严重,它可能有更好的设计解决方案:)@Luggi,我刚刚更新了我的问题,如果仍然令人困惑,请告诉我。我理解:)很难从问题中看出实际需求是什么:/