I have two hashmap
LinkedHashMap<String, int[]> val1 = new LinkedHashMap<String, int[]>();
LinkedHashMap<String, int> val2 = new LinkedHashMap<String, int>();
each hashmap has different key and values. I am trying to iterate over both hashmap
at the same time and multiply each value of val1->int[] to val2->int
What is the easiest and fasted way to do it? I have thousands values in both hashmap.
Thanks
-
2Hmm, if they have different keys, then what does it mean to iterate over them at the same time? Say val1 has keys "a" and "b", while val2 has keys "X" and "Y". Does that mean you want to handle all four combinations (aX, aY, bX, bY), or what?Vance Maverick– Vance Maverick03/27/2011 04:22:19Commented Mar 27, 2011 at 4:22
-
1Example versions of val1 and val2 with expected output will go a long way.Andrew White– Andrew White03/27/2011 04:24:26Commented Mar 27, 2011 at 4:24
-
A LinkedHashMap has a iteration ordering, so, if he iterates from front to end, there's only one way he/she can do it.Dhaivat Pandya– Dhaivat Pandya03/27/2011 04:29:16Commented Mar 27, 2011 at 4:29
-
logically val1 and val2 have relationship. val1->int[] contains multiple months' expense and val2->int contains the number of people. Now I want to iterate to multiple each month expense with number of people to get some values for further processing. val1 and val2 will always have same sizeTweet– Tweet03/27/2011 04:35:22Commented Mar 27, 2011 at 4:35
2 Answers 2
You are probably doing it wrong...
First, a HashMap can't store ints, it needs proper objects - like Integer – An array is an object, although it's hidden behind some syntactic sugar.
Here's how to loop over both maps, if they happens to have the same size, which is what I think you mean.
Iterator<int[]> expenses = val1.values().iterator();
Iterator<Integer> people = val2.values().iterator();
assert val1.size() == val2.size() : " size mismatch";
while (expenses.hasNext()) {
int[] expensesPerMonth = expenses.next();
int persons = people.next();
// do strange calculation
int strangeSum = 0;
for (int idx = 0; idx < expensesPerMonth.length; idx++) {
strangeSum += persons * expensesPerMonth[idx];
}
System.out.println("strange sum :" + strangeSum);
}
But You should probably go back and rethink how you store your data – why are you using maps, and whats the key?
Wouldn't it be better to create an object that represents the combination of monthly expenses and number of people, for instance?
-
Thanks, I know it is wrong logic but I am not main developer. I am just maintaining.Tweet– Tweet03/27/2011 14:52:02Commented Mar 27, 2011 at 14:52
AFAIK, a LinkedHashMap has iteration ordering. So, something like this may work:
Iterator myIt1 = val1.entrySet().iterator();
Iterator myIt2 = val2.entrySet().iterator();
while(val1.hasNext() && val2.hasNext()) {
int myarray[] = val1.next();
for(int i = 0; i<myarray.length; i++) {
myarray[i] = myarray[i] * val2.next();
}
}
-
for using val2.next(), it gives an error that The method next() is undefined for the type LinkedHashMap<String,int[]>Tweet– Tweet03/27/2011 04:38:44Commented Mar 27, 2011 at 4:38
-
You're advancing iterator2 too often. (At least according to my interpretation of the question)I82Much– I82Much03/27/2011 04:38:48Commented Mar 27, 2011 at 4:38
-
1Could you please check your code again? it has several errors :(Tweet– Tweet03/27/2011 04:50:35Commented Mar 27, 2011 at 4:50