首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >具有键和值的HashMap等于使用Java的另外两个哈希图的值

具有键和值的HashMap等于使用Java的另外两个哈希图的值
EN

Stack Overflow用户
提问于 2012-04-06 01:43:17
回答 3查看 1.8K关注 0票数 0

我有两个散列映射,我想填充第三个散列映射,这些散列映射的键将是第一个散列映射的值,这些值将是拆分到一个数组中的第二个散列映射的值。即:

代码语言:javascript
复制
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}

这就是我到目前为止所做的:

代码语言:javascript
复制
  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]);
        }


       }

    }

但是输出结果是:

代码语言:javascript
复制
hashmap3 = {e1=word1-word2-word3, e2=word1-word2-word3}

有什么想法吗?

EN

Stack Overflow用户

发布于 2012-04-06 04:20:18

根据你的例子,你有:

代码语言:javascript
复制
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}

hashmap1和hashmap2之间没有公共键,所以我们尝试将键为"1“的hashmap1中的值与键为"10”的hashmap2中的值关联起来。除非保留有关如何将条目从hashmap1映射到hashmap2的附加信息,否则无法做到这一点。如果使用保证迭代顺序与插入顺序相同的映射(例如LinkedHashMap),则此附加信息可以是映射中的插入顺序。

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10033336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档