简体   繁体   English

HashMap 键和值等于使用 Java 的其他两个哈希图的值

[英]HashMap with key and value equal the values of two other hashmaps using Java

I have two hashmaps and I would like to fill a third hashmap which keys will be the values of the first hash map and the values will be the values of the second hashmap splitted to an array.我有两个散列映射,我想填充第三个 hashmap ,其中键将是第一个 hash map 的值,值将是第二个 hashmap 的值拆分为一个数组。 ie: IE:

hashmap1 = {1=e1, 2=e2}
hashmap2 = {10=word1-word2-word3, 20=word4-word5-word6}
the result:
hashmap3 = {e1=word1-word2-word3, e2=word4-word5-word6}

This is what I did so far:这是我到目前为止所做的:

  static HashMap<Integer, String> catnamecatkeys = new HashMap<Integer, String>();

    static HashMap<Integer, String> keywords = new HashMap<Integer, String>();

    static HashMap<String, String> tempHash = new HashMap<String, String>();

    static HashMap<String, String[]> hash = new HashMap<String, String[]>();

    static String[] arr;

    public static void main(String[] args) {

    catnamecatkeys.put(1, "e1");
    catnamecatkeys.put(2, "e2");
    keywords.put(1, "word1-word2-word3");
    keywords.put(2, "word4-word5-word6");

    for (int key : catnamecatkeys.keySet()) {
        tempHash.put(catnamecatkeys.get(key),null);
    }

    for(String tempkey: tempHash.keySet()){          
        tempHash.put(tempkey,keywords.entrySet().iterator().next().getValue());
        arr = tempHash.get(tempkey).split("-");
        hash.put(tempkey, arr);
    }
    System.out.println(tempHash);
    for (String hashkey : hash.keySet()) {
        for (int i = 0; i < arr.length; i++) {
            System.out.println(hashkey + ":" + hash.get(hashkey)[i]);
        }


       }

    }

but the output is:但是 output 是:

hashmap3 = {e1=word1-word2-word3, e2=word1-word2-word3}

Any Ideas please?有什么想法吗?

Your problem is this line:你的问题是这一行:

keywords.entrySet().iterator().next().getValue()

is always going to return the same entry of the keywords HashMap. Try building your new hashmap with something like:总是会返回keywords HashMap 的相同条目。尝试使用类似以下内容构建新的 hashmap:

for (int i = 1; i < 3; i++) {
    tempHash.put(catnamecatkeys.get(i), keywords.get(i));
}

You should initialize Iterator outside the loop, Here is complete example -您应该在循环外初始化 Iterator,这是完整的示例 -

static HashMap<Integer, String> catnamecatkeys = new HashMap<Integer, String>();

static HashMap<Integer, String> keywords = new HashMap<Integer, String>();

static HashMap<String, String> tempHash = new HashMap<String, String>();

static HashMap<String, String[]> hash = new HashMap<String, String[]>();

static String[] arr;
public static void main(String[] agrs)
{     
   catnamecatkeys.put(1, "e1");
        catnamecatkeys.put(2, "e2");
        keywords.put(1, "word1-word2-word3");
        keywords.put(2, "word4-word5-word6");

        for (int key : catnamecatkeys.keySet()) {
            tempHash.put(catnamecatkeys.get(key),null);
        }
     Set<Entry<Integer,String>> set =  keywords.entrySet();
      Iterator<Entry<Integer, String>>  iterator= set.iterator();
        for(String tempkey: tempHash.keySet()){          
            tempHash.put(tempkey,iterator.next().getValue());
            arr = tempHash.get(tempkey).split("-");
            hash.put(tempkey, arr);
        }
        System.out.println(tempHash);
        for (String hashkey : hash.keySet()) {
            for (int i = 0; i < arr.length; i++) {
                System.out.println(hashkey + ":" + hash.get(hashkey)[i]);
            }


           }
}

According to your example where you have:根据您的示例,您有:

hashmap1 = {1=e1, 2=e2}
hashmap2 = {10=word1-word2-word3, 20=word4-word5-word6}
the result:
hashmap3 = {e1=word1-word2-word3, e2=word4-word5-word6}

There is no common key between hashmap1 and hashmap2, so we are trying to relate the value from hashmap1 with key "1" to the value in hashmap2 with key "10". hashmap1 和 hashmap2 之间没有公共键,因此我们试图将键为“1”的 hashmap1 中的值与键为“10”的 hashmap2 中的值相关联。 There is no way to do this unless additional information about how to map the entries from hashmap1 to hashmap2 is retained.除非保留有关如何 map 从 hashmap1 到 hashmap2 的条目的附加信息,否则无法执行此操作。 This additional information could be the insertion order into the map if a map that guarantees iteration order to be the same as insertion order is used (eg LinkedHashMap).如果使用保证迭代顺序与插入顺序相同的 map(例如 LinkedHashMap),则此附加信息可以是插入顺序到 map。

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

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