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

The Archive Base Latest Questions

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

Rather than attempt this in words, I’ll just give an example: I have an

  • 0

Rather than attempt this in words, I’ll just give an example:

I have an Animal class, as well as a Dog,Fish,Cat, etc. which extend Animal.

I have three different methods, which return Map<String,List<Dog>>, Map<String,List<Fish>>, Map<String,List<Cat>>. We’ll call these getDogMap, getCatMap, and getFishMap.

I am writing a generic method which, depending on various parameters, calls one of these methods. Here is what I expected to be allowed to do:

public void <A extends Animal> doSomething(){

    Map<String,List<A>> someMap;

    if(someCondition){
        someMap = getDogMap();
    }else if(anotherCondition){
        someMap = getFishMap();
    }else{
        someMap = getCatMap():
    }
}

Or at least, that with casting ala someMap = (Map<String,List<Dog>>) getDogMap();

However, this does not work. Eclipse tells me "Type mismatch: cannot convert from Map<String,List<Dog>> to Map<String,List<A>>" If I try to force the cast, it tells me "Cannot cast from Map<STring,List<Dog>> to Map<String,List<A>>".

What am I doing wrong?

  • 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-09T23:23:34+00:00Added an answer on June 9, 2026 at 11:23 pm

    public void <A extends Animal> doesn’t mean “A is any type that extends Animal“, it means “A is a specific one of the types that extends Animal“. You need to use the following declarations:

    public void doSomething() {
        Map<String, ? extends List<? extends Animal>>  someMap;
        // ...
    }
    

    The construct ? extends Animal is how you express “any type that extends Animal“.

    The reason you have to use that declaration is that, counter-intuitively, the way subtype relationships between generic types work isn’t exactly consistent with how they work between regular types. For instance, List<Dog> is a subtype of Collection<Dog>. It is not a subtype of List<Animal>, or Collection<Animal> etc. The reason why this isn’t allowed is called heap pollution, also explained in Angelika Langer’s FAQ. List<Dog> is, however, a subtype of List<? extends Animal>. A variable of type List<? extends Animal> may have assigned a List<Dog>, or a List<Cat>, or a List<Animal>. The important part is that the compiler doesn’t know which of these it is, just that it is one of them.

    Just like List<Dog> is not a subtype of List<Animal>, analogously it holds that Map<String, List<Dog>> is not a subtype of Map<String, List<? extends Animal>>.

    The best way to demonstrate why generics work this way is proof by contradiction; that is, showing examples of (broken) code that would lead to errors were generics to work “intuitively”. So, if List<Dog> were a subtype of List<Animal>, the following code would be valid:

    List<Dog> dogs = new ArrayList<Dog>();
    List<Animal> animals = dogs; // unsafe cast
    
    // this operation violates type safety
    animals.add(new Cat());
    
    // would assign a Cat to a variable of type Dog without a compile error!
    Dog dog = animals.get(0);
    

    Similarly, for your Map:

    Map<String, List<Dog>> dogses = new HashMap<String, List<Dog>>();
    Map<String, List<? extends Animal>> animalses = dogses; // unsafe cast
    
    List<Cat> cats = new ArrayList();
    cats.put(new Cat());
    animalses.put("cats", cats);
    
    List<Dog> dogs = dogses.get("cats");
    Dog dog = dogs.get(0); // uh-oh
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Rather than just grabbing the user's friends' emails, I want to show their list
Rather than have everything in one big dialog, I'm looking at being able to
Rather than have a NULL in a column, I want a 0 to be
Suppose that I have two sets of associated types, for example Animal s and
There are some rare data-corruption circumstances where rather than attempt a recovery in the
Rather than using WMI to obtain the last boot time, I wanted to calculate
rather than using a textnode to show content im trying to figure out how
Rather than reinvent the wheel I would like to use Outlook to manage my
Rather than doing my_var = my_var+'extra string'; is there a shorthand method like .=
Rather than pushing a release to PyPi and GitHub, it would be easier to

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.