1

I'm struggling with a little bit of code, and I just cannot figure out what the correct syntax would be. Here is an example of the problem:

for (Integer key : map1.keySet()) {
 for (Integer value : map2.values()) {
 if (value == key) {
 // ++ the corresponding value of the looping map1 key.
 // tried statements like map1.values()++ 
 // but this is an invalid operation.
 }
 }
}

I am trying to loop map1 keys through map2 values and if within this process a map1 key matches a map2 value, I want to add one to the value of the corresponding map1 key.

Should be a simple problem but Eclipse keeps telling me that I have the syntax wrong, can anyone suggest what statement I may need to iterate the values?

asked Dec 20, 2013 at 18:00
2
  • 1
    please add Eclipse syntax error Commented Dec 20, 2013 at 18:02
  • A stacktrace would be great. Commented Dec 20, 2013 at 18:06

4 Answers 4

2

Here is how you can do it with a very small modification to your code:

for (Map.Entry<Integer,Integer> entry : map1.entrySet()) {
 for (Integer value : map2.values()) {
 if (value.equals(entry.getKey())) {
 entry.setValue(entry.getValue()+1);
 }
 }
}

EDIT : Since map2 could contain the same value more than once, the other solution that uses a hash set to speed things up will not work as expected.

answered Dec 20, 2013 at 18:11
4
  • Thanks for the help! with this set up I am getting just 1 for the values of map1. I need plus 1 for each time a map1 key equals a map2 value. Do I just need to move the "+1" elsewhere? Commented Dec 20, 2013 at 18:28
  • @user2941526 I see - map2 can have the same value multiple times, right? Then you should the version with two loops. Commented Dec 20, 2013 at 18:31
  • You should probably use .equals, not ==, since you're working with boxed Integer objects. Commented Dec 20, 2013 at 22:00
  • @LouisWasserman Absolutely! Thank you very much - I copy/pasted the code, and missed this important issue. Great spotting! Thanks! Commented Dec 20, 2013 at 22:02
0

How you init yours maps? Like Map<Integer, Integer> map1=new LinkedHashMap<Integer, Integer>(); or Map map1=new LinkedHashMap(); If the second way the key will be Object not Integer. And in all cases compare two Integer using == bad practise. Use equals

answered Dec 20, 2013 at 18:12
0

I'm not sure why Eclipse report a syntax error, but in any case this loop does not make much sense, consider changing it to:

for (Integer value : map2.values())
 if (map1.containsKey(value))
 // bla bla

it will run in O(n) rather than the former O(n*m)

when n is the size of map2 and m is the size of map1 (as notified by @dasblinkenlight)

answered Dec 20, 2013 at 18:05
4
  • I believe a map should find in and average of O(1) Commented Dec 20, 2013 at 18:09
  • @dasblinkenlight pls see stackoverflow.com/questions/1055243/is-a-java-hashmap-really-o1 Commented Dec 20, 2013 at 18:11
  • yes, as you can see I look for the key there, thats why I switched the order of the blocks Commented Dec 20, 2013 at 18:13
  • Gee, you're right, I did not notice that you've switched the order of the loops. You should mention that n is the size of map2, because the sizes of the two maps may differ. Commented Dec 20, 2013 at 18:15
0

You can use containsKey method like this:

for (Integer value : map2.values()){
if (map1.containsKey(value))
 map1.put(key, value);
...
}
answered Dec 20, 2013 at 18:12

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.