Generic Map for each loop problem

Discussion in 'Java' started by Mize-ze, Dec 21, 2006.

  1. Mize-ze

    Mize-ze Guest

    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
     
    Mize-ze, Dec 21, 2006
    #1
    1. Advertising

  2. Mize-ze

    Hemal Pandya Guest

    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
     
    Hemal Pandya, Dec 22, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,049
    Chaitanya
    Feb 3, 2009
  2. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    429
    Tjerk Wolterink
    Nov 3, 2004
  3. easy
    Replies:
    8
    Views:
    397
  4. pinky

    generic map problem

    pinky, Feb 9, 2009, in forum: VHDL
    Replies:
    0
    Views:
    705
    pinky
    Feb 9, 2009
  5. Isaac Won
    Replies:
    9
    Views:
    381
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page