Generic Map for each loop problem

M

Mize-ze

Why can't I compile the following code?

Map bestUtilities;

.....

for(Map.Entry<Edge,ActionItem> me : bestUtilities.entrySet())
{
// Some stuff
}


I guessed at first that I cannot compile it because the bestUtilities
Map is not Generic and when I try to iterate on its entrySet with a
generic entry I get this incompatability/conversion problem
BUT when I tried simply Map.Entry it did not compile too.

It [eclipse 3.2]says:
"Type mismatch: cannot convert from element type Object to
Map.Entry<Edge,ActionItem>"

What am I missing here?
Thanks
 
H

Hemal Pandya

Mize-ze said:
Why can't I compile the following code?

I couldn't compile it even after fixing your errors, because Edge and
ActionItem are not defined. Look up SSCCE; even when you are talking
about compile errors, it is best to ensure the rest of the code is
SSCCE.
Map bestUtilities;

....

for(Map.Entry<Edge,ActionItem> me : bestUtilities.entrySet())

for(Map.Entry<Edge,ActionItem> me :
((Map<Edge,ActionItem>) bestUtilities).entrySet())

You will get a warning for unchecked cast, but presumably you know the
correct type of bestUtilities.
{
// Some stuff
}


I guessed at first that I cannot compile it because the bestUtilities
Map is not Generic and when I try to iterate on its entrySet with a
generic entry I get this incompatability/conversion problem
BUT when I tried simply Map.Entry it did not compile too.

It would have been better if you had not decided to keep the actual
code secret. Now I will have to guess...
It [eclipse 3.2]says:
"Type mismatch: cannot convert from element type Object to
Map.Entry<Edge,ActionItem>"

Do you mean like this?
Map.Entry<Edge,ActionItem> meToo = (Map.Entry<Edge,ActionItem>) me;

This too gives me unchecked warning, not error. If this not what your
code looks like, my guess in incorrect and you need to show the actual
code.
What am I missing here?

Code copy (You probably guessed it :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top