Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Generic Map for each loop problem (http://www.velocityreviews.com/forums/t389604-generic-map-for-each-loop-problem.html)

Mize-ze 12-21-2006 11:37 PM

Generic Map for each loop problem
 

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


Hemal Pandya 12-22-2006 04:18 AM

Re: Generic Map for each loop problem
 
Mize-ze wrote:
> 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 :-).

> Thanks




All times are GMT. The time now is 08:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.