Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generic Map for each loop problem

Reply
Thread Tools

Generic Map for each loop problem

 
 
Mize-ze
Guest
Posts: n/a
 
      12-21-2006

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

 
Reply With Quote
 
 
 
 
Hemal Pandya
Guest
Posts: n/a
 
      12-22-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
generic map problem pinky VHDL 0 02-09-2009 03:50 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
ReadWriteLock rather on map's each field than whole map? easy Java 8 09-29-2007 07:45 PM
xsl:for-each for each 3 elements problem Tjerk Wolterink XML 3 11-03-2004 05:22 PM



Advertisments