0

我有两个散列图,我想填充第三个散列图,其中键将是第一个散列图的值,值将是第二个散列图的值拆分为数组。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}

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

  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}

请问有什么想法吗?

4

3 回答 3

1

你的问题是这一行:

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

总是要返回keywordsHashMap 的相同条目。尝试使用以下内容构建新的哈希图:

for (int i = 1; i < 3; i++) {
    tempHash.put(catnamecatkeys.get(i), keywords.get(i));
}
于 2012-04-05T17:56:40.920 回答
1

您应该在循环外初始化迭代器,这是完整的示例 -

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


           }
}
于 2012-04-05T18:04:16.730 回答
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),则此附加信息可以是映射中的插入顺序。

于 2012-04-05T20:20:18.433 回答