Multiple Keys to Single Value Map Java

后端 未结 4 1032
忘了有多久
忘了有多久 2020-12-03 10:47

I think my question is similar to this one: How to implement a Map with multiple keys? but with an important difference. In that question (if my understanding of it is corre

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 10:58

    It sounds to me like you're looking for a nested HashMap. This could work, but my gut says implementing such a monster would be a terrible idea, both performance-wise and sanity-wise.

    How you could initialize it:

        HashMap> nestedHashMap = new HashMap>();
    

    Adding values:

        Key1 first;
        Key2 second;
        Value data;
        HashMap tempMap = new HashMap();
        tempMap.put(second, data);
        nestedHashMap.put(first, tempMap);
    

    Getting data back out:

        Key1 first;
        Key2 second;
        Value data;
        data = nestedHashMap.get(first).get(second);
    

    Disclaimer: This code hasn't been tested, it just came off the top of my head.

提交回复
热议问题