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.
i.e.:
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:
hashmap3 = {e1=word1-word2-word3, e2=word1-word2-word3}
Any Ideas please?
You should initialize Iterator outside the loop, Here is complete example –