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 8737501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:32:12+00:00 2026-06-13T10:32:12+00:00

Why does trying to compile public class GenericsFail { public static void main(String[] args)

  • 0

Why does trying to compile

public class GenericsFail {
    public static void main(String[] args) {
        accept(new HashMap<String, List<String>>());
    }

    public static void accept(Map<String, List<?>> multiMap) {}
}

give the error

GenericsFail.java:7: error: method accept in class GenericsFail cannot be applied to given types;
                accept(new HashMap<String, List<String>>());
                ^
  required: Map<String,List<?>>
  found: HashMap<String,List<String>>
  reason: actual argument HashMap<String,List<String>> cannot be converted to Map<String,List<?>> by method invocation conversion

The wildcard is only allowed if it’s not nested inside List.

  • 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-13T10:32:14+00:00Added an answer on June 13, 2026 at 10:32 am

    The reason is that the ? in List<?> could be “anything”, but a different “anything” in each Map entry. That is, it would accept a List<String> in one entry, and a List<Integer> in another.

    But you are passing in a Map that has the same type of List in every entry, so the type is not bound in the same way or the to same degree for freedom.

    The “fix” is to lock the type to a specific type, but still being “anything” – just the same “anything* in every entry, by typing the method:

    public static <T> void accept(Map<String, List<T>> multiMap) // complies
    

    or if your method really doesn’t need to know which type, use a wildcard to wrap the type:

    public static void accept(Map<String, ? extends List<?>> multiMap) // compiles
    

    This last version works because the type of the list, although being a wildcard, is fixed to an unknown, but consistent, type when called.

    I find the typed version easier to read (and code), and the type is there for use should you decide later that your method needs to know the type.

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

Sidebar

Related Questions

I'm trying to compile the following code: public class BaseRequest<TResponse> where TResponse : BaseResponse
I am trying to delete part of a string that does not match my
I'm trying to compile class A, which has a member of class B, where
I have the following class... public class MyCustomLogger { public static int DEFAULT_INFO =
Let's look at the simple Java code in the following snippet: public class Main
I'm new to java and I'm trying to compile and run a web service
Here is my class: public class A{ private void doIt(int[] X, int[] Y){ //change
I have a C# class defined as follows: public class GenericItem<T> { public List<T>
In trying to compile this program: namespace MyNamespace { template<typename T> class Test {
I am getting errors trying to compile a C++ template class which is split

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.