2

I've got a basic HashMap. I'm trying to loop over it and get both the key and the value out of the Map. Here's what I have:

Map<String, String> myMap = versionExtractor.getVersionInfo();
for(String key : myMap.keySet())
    System.out.println(key);
    System.out.println(myMap.get(key));
}

The problem is that this won't compile. There is an error on the line that says System.out.println(myMap.get(key)); that says:

java: class, interface, or enum expected

And the intelliJ IDE says: Cannot resolve symbol 'key'. The perplexing thing is that is resolved key without a problem in the preceding line that says System.out.println(key);. What's up with that?

5
  • I've updated my question with the correct code for reading values out of HashMaps. Commented Jan 10, 2014 at 17:13
  • Please don't change your original question, as it now makes all the current answers void. Better to update it, leaving the original there only. Commented Jan 10, 2014 at 17:18
  • The question hasn't changed, I just fixed a typo in the syntax. The question remains. Commented Jan 10, 2014 at 17:19
  • 1
    Ha... where's my opening {? lol... oh geez. Commented Jan 10, 2014 at 17:22
  • @quakkels you could add that as an answer and accept that :) Commented Jan 10, 2014 at 17:24

6 Answers 6

4

Instead of myMap[key], you should use myMap.get(key). It's a Map, not an array. BTW, if you want both key and value, you can rather iterate over the EntrySet:

for (Entry<String, String> entry: myMap.entrySet()) {
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
}

This saves extra hash calculation, and lookup on every iteration.


As it goes, the issue is something else. The missing { brace after your for statement, makes the closing } of for loop extraneous. Anyways, my original answer was with regards to your original question which used myMap[key] instead.

Sign up to request clarification or add additional context in comments.

Comments

3

In order to get a corresponding value for a given key, you should use the get(Object key) method:

for(String key : myMap.keySet())
    System.out.println(key);
    System.out.println(myMap.get(key));
}

Even better, in order to iterate a Map, you can use the Map's static Entry class:

for (Map.Entry<String, String> entry: map.entrySet()) {
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
}

1 Comment

I've updated my question. the problem persists. I cannot use key more than once in the loop.
2

You're missing the opening brace on your for statement, so your second println is not part of the for loop.

Comments

2

You're treating your Map like an array. That's not how Maps work. Use the Map.get(key) function instead.

For more info, check out the tutorial, which is the first result for googling "java maps".

Comments

1

Given that you've got this:

java: class, interface, or enum expected

I wonder if you're compiling with JDK 1.4, and you don't have the enhanced for loop of Java 5 available.

Once you've solved that, don't you want to loop over the Map.Entry objects, to avoid a redudant value lookup via get() ?

for (Map.Entry<String,String> entry : map.entrySet()) {
   ...

2 Comments

I tried that with the same results. the entry will work the first time I use it but not the second.
I'm using 1.7... I found it. I'm missing an opening { after the for() statment. (smack myself in the forehead.)
0

try it :

Map.get(key)

Or

alternative look this example:http://www.mkyong.com/java/how-to-loop-a-map-in-java/

1 Comment

No you cannot , It will look for "key" key in the map and fails.

Your Answer

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.