1

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

Mogsdad
45.9k21 gold badges164 silver badges286 bronze badges
asked Mar 27, 2011 at 3:54
4
  • 2
    Hmm, 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? Commented Mar 27, 2011 at 4:22
  • 1
    Example versions of val1 and val2 with expected output will go a long way. Commented 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. Commented 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 size Commented Mar 27, 2011 at 4:35

2 Answers 2

1

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?

answered Mar 27, 2011 at 9:07
1
  • Thanks, I know it is wrong logic but I am not main developer. I am just maintaining. Commented Mar 27, 2011 at 14:52
0

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();
 }
}
answered Mar 27, 2011 at 4:35
3
  • for using val2.next(), it gives an error that The method next() is undefined for the type LinkedHashMap<String,int[]> Commented Mar 27, 2011 at 4:38
  • You're advancing iterator2 too often. (At least according to my interpretation of the question) Commented Mar 27, 2011 at 4:38
  • 1
    Could you please check your code again? it has several errors :( Commented Mar 27, 2011 at 4:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.