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

  • Home
  • SEARCH
  • 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 8812559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:37:57+00:00 2026-06-14T03:37:57+00:00

static <V> void myMethod(Map<?, V> map) { Iterator<Entry<?, V>> it = map.entrySet().iterator(); } I’m

  • 0
static <V> void myMethod(Map<?, V> map)
{
 Iterator<Entry<?, V>> it = map.entrySet().iterator();
}

I’m seeing below compilation error:
Type mismatch: cannot convert from Iterator<Map.Entry<capture#5-of ?,V>> to Iterator<Map.Entry<?,V>>

  • 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-14T03:37:58+00:00Added an answer on June 14, 2026 at 3:37 am

    Try

    Iterator<? extends Entry<?, V>> it = map.entrySet().iterator();
    

    The reason your attempt doesn’t work is a bit hard to see, particularly because Iterator<T> does not consume any T (i.e. it doesn’t have a method which takes a T as a parameter).

    You can’t assign an Iterator<Entry<capture#5-of ?,V>> to an Iterator<Entry<?, V>> for the same reason that you can’t assign an Iterator<Entry<Integer, String>> to an Iterator<Entry<?, V>>. The capture#5 is just a name used to differentiate the specific ? found in your method’s parameter from other distinct wildcard instances. It could just as easily be a concrete type.

    The reason this doesn’t work is more clear if instead of Iterator you think of a class like List.

    List<Entry<Integer, String>> entries = new ArrayList<>();
    
    //this is a compile error, but assume it is possible
    List<Entry<?, String>> wildcardEntries = entries; 
    
    //then since this is already possible
    wilcardEntries.add(new Entry<String, String>("a", "b"));
    Entry<Integer, String> entry1 = entries.get(0);
    
    //this would result in a type error (ClassCastException)
    Integer i = entry1.getKey();
    

    By using ? extends Entry<?, V> you’re not exposing yourself to this, as you do not claim to know anything about the type of Entry your Iterator can consume, only what it can produce.

    Edit

    Though Jiman deleted his answer, he had a good point that using the for-each loop is a lot cleaner approach (assuming you’re just trying to iterate over the entries) that would avoid this issue entirely. This should work:

    for ( Entry<?, V> entry : map.entrySet() ) {
       //...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you clarify this please? public static void MyMethod() { var context= new MyModel.Entities();
I have a pattern like - public static void myMethod(int Val , String Val){}
This is a contrived example: public static class MyExtensions { public static void MyMethod(
Lets say i have a method public static void Blah(object MyMethod) { // i
Ok, this is what I want to do: public static void CallStaticMethod(Type myType, String
Here's my code: class FinallyDemo { static void myMethod(int n) throws Exception{ try {
Using Play Framework, you can do something like this: public static void mymethod() {
If I do this in c# 4.0 .NET private static void myMethod<T>(int obj) where
public class Myclass{ public static void Mymethod{ //i want to go to an Activity
Following is my method to append one file onto another file. public static void

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.