带有键和值的HashMap等于使用Java的其他两个哈希映射的值

时间:2012-04-05 17:43:17

标签: java hashmap

我有两个哈希映射,我想填充第三个哈希映射,其中键将是第一个哈希映射的值,值将是分割为数组的第二个哈希映射的值。 即:

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}

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

  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]);
        }


       }

    }

但输出是:

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

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

你的问题就在这一行:

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

总是会返回keywords HashMap的相同条目。尝试使用以下内容构建新的hashmap:

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

答案 1 :(得分:1)

你应该在循环之外初始化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]);
            }


           }
}

答案 2 :(得分:0)

根据您的例子:

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之间没有公共密钥,因此我们尝试将hashmap1的值与密钥“1”关联到hashmap2中的值为“10”的值。除非保留有关如何将条目从hashmap1映射到hashmap2的其他信息,否则无法执行此操作。如果使用保证迭代顺序与插入顺序相同的映射(例如LinkedHashMap),则此附加信息可以是插入到地图中的顺序。