Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9301971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:09:00+00:00 2026-06-18T23:09:00+00:00

I need to iterate over the entry set of a map from which I

  • 0

I need to iterate over the entry set of a map from which I do not know its parameterized types.

When iterating over such entryset, why this does not compile ?

public void myMethod(Map anyMap) {
    for(Entry entry : anyMap.entrySet()) {
        ...
    }
}

but this compile:

public void myMethod(Map anyMap) {
    Set<Entry> entries = anyMap.entrySet();
    for(Entry entry : entries) {
        ...
    }
}

and this also compiles (I cannot use this one since I do not know the types of the map):

public void myMethod(Map<String, String> stringMap) {
    for(Entry<String,String> entry : stringMap.entrySet()) {
        ...
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:09:01+00:00Added an answer on June 18, 2026 at 11:09 pm

    The error you get in your first one is:

    Type mismatch: cannot convert from element type Object to Map.Entry
    

    This is because the compiler converts your FOR-IN loop:

    for (Entry entry : anyMap.entrySet()) {
    }
    

    To:

    for (Iterator i = anyMap.entrySet().iterator(); i.hasNext();) {
        Entry e = i.next(); // not allowed
    }
    

    Your second example works, but only through cheating! You are doing an unchecked cast to get Set back into a Set<Entry>.

    Set<Entry> entries = anyMap.entrySet(); // you get a compiler warning here
    for (Entry entry : entries) {
    }
    

    Becomes:

    Set<Entry> entries = anyMap.entrySet();
    for (Iterator<Entry> i = entries.iterator(); i.hasNext(); ) {
        Entry e = (Entry) i.next(); // allowed
    }
    

    Update

    As mentioned in comments, the type information is getting lost in both examples: because of the compiler’s raw type erasure rules.

    To provide backwards compatibility, ALL methods of raw type instances are replaced by their erased counterparts. So, because your Map is a raw type, it all gets erased. Including its Set<Map.Entry<K, V>> entrySet(); method: your raw type instance will be forced to use the erased version: Set entrySet().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to iterate over a recordset from a stored procedure and execute another
Possible Duplicate: How do I iterate over each Entry in a Map? I am
I need to iterate over an Object, but know when I'm on the last
I have a class with multiple vectors from measurements. I need to iterate over
I need to iterate over a vector from the end to the beginning. The
I have a JSON file which I need to iterate over, as shown below...
I have a Data.Sequence which I need to iterate over. The problem is that
I need to iterate over the vertices and edges of a BGL adjacency_list from
I need to iterate over an array for which the keys are non-consecutive: var
I have 5 buttons and I need to iterate over them to carry out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.