具有具有多个值的多个键的哈希图 [英] Hashmaps having multiple keys with multiple values

查看:160
本文介绍了具有具有多个值的多个键的哈希图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.ini文件是否可以具有多个键和多个值?如果可以,怎么办?

Is it possible for me to have multiple keys and multiple values for my .ini file? if so how?

我知道我们可以使用列表或多重映射使用具有多个值的单个键,但是我可以为多个键复制相同的概念 像这样的键-a,b,c 值-(对于第1行)d,e,f等,对于多行 我真正想要的是我的ini文件

i know we can have single key with multiple values using list or multimaps but can i replicate the same concept for multiple keys like this-keys-a,b,c values-(for row 1)d,e,f and so on for multiple rows what i actually want is this for my ini file

   key1||key2||key3
   value1||value2||value3
   value4||value5||value6

key1映射到value1和4,依此类推

key1 maps to value1 and4 and so on

所有这些都应该放在一张地图中

and all of this should be in one map

推荐答案

重新进行编辑,看起来您想通过任何键进行检索.因此,您真正拥有的只是一个String : List地图.

Re your edit, it looks like you want to retrieve by any of the keys. So all you really have here is a String : List map.

只需遍历该文件,在||上拆分每一行,然后将第一个条目添加到第一个键的列表中,将第二个条目添加到第二个键的列表中,依此类推.

Just loop through that file, split each line on ||, and add the first entry to the first key's list, the second entry to the second key's list, etc.

非常类似于Java的伪代码:

Very roughly, Java-like pseudocode:

Map<String, List> map = HashMap<String, List>();
String line;
String[] entries;
int index;
String[] keys;

//...open file...

// First line is keys
line = source.readLine();
if (line == null) {
    // File is empty
}
else {
    // Add the keys
    keys = new String[entries.size()];
    index = 0;
    for (String key : entries) {
         keys[index++] = key;
         map.put(key, new LinkedList());
    }

    // Process entries
    while ((line = source.readLine()) != null) {
        entries = line.split("\\|\\|");
        index = 0;
        for (String entry : entries) {
            if (index >= keys.length) {
                break;
            }
            map.get(keys[index++]).add(entry);
        }
    }
}

...或类似的东西.

...or something like that.

原始答案,仅适用于以后发现此问题并想另辟way径的人:

Original answer, just for anyone finding this later who wants to go another way:

我可以通过两种方式阅读您的问题:

I can read your question two ways:

如果您想将List存储在多个键下并使用其中任何一个来检索它,则可以执行以下操作:

If you mean you want to store a List under several keys and use any one of them to retrieve it, you can do that:

map.put(key1, list);
map.put(key2, list);
map.put(key3, list);

// ...later
list = map.get(key1); // or key2, or key3

如果您想使用 all 的所有键而不是单个键进行检索,那么从概念上讲,这只是一个键.您可以从任何MapList类派生,根据您对相同"键的定义添加自己的equalsgetHashCode,并使用:

If you mean you want to retrieve using all of the keys but not the keys individually, then conceptually that's just one key. You could derive from any of the Map or List classes, add your own equals and getHashCode as appropriate to your definition of the "same" key, and use that:

map.put(specialMultiKey, list);

// ...later
list = map.get(specialMultiKey);

这篇关于具有具有多个值的多个键的哈希图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆