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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:06:07+00:00 2026-05-29T23:06:07+00:00

I am trying to determine if it is possible to use Generics in the

  • 0

I am trying to determine if it is possible to use Generics in the following situation. It can best be described by my code. (This code is just an example; I took out most of the code not relevant to the problem I’m having.)

public class FooBar {

    public <T extends MyModel> Map<Class<T>, List<T>> convertToModelList(
            Map<String, Class<T>> infoMap) {

        // do stuff...
    }
}

public class MyClient {

public void doSomething() {
    Map<String, Class<? extends MyModel>> oldMap = new HashMap<String, Class<? extends MyModel>>();
    oldMap.put ("car", Car.class);
    oldMap.put("truck", Truck.class);

    FooBar f = new FooBar();
    Map<Class<? extends MyModel>, List<? extends MyModel>> newMap = f
            .convertToModelList(oldMap);
}
}

public class Car extends MyModel {

}

public class Truck extends MyModel {

}

public class MyModel { 

}

The compiler is saying that I can’t call convertToModelList (in MyClient) because a Map<String, Class<? extends MyModel>> is not equivalent to Map<String, Class<T>>. I somewhat understand why this is occurring, but is there a way around this?

EDIT:

To be more specific, the problem I’d like to solve is using Generics in the convertToModelList() method above. If I can’t use generics here, then whatever I return from that method has to be cast on the client. For example, if I change FooBar to this:

public class FooBar {

    public Map<Class<? extends MyModel>, List<? extends MyModel>> convertToModelList(
            Map<String, Class<? extends MyModel>> infoMap) {

        // do stuff...
    }
}

If I pass in a

Map<String, Class<Truck>> 

to convertToModelList, it will return a

Map<Class<Truck>, List<Truck>>

, however the client won’t know it’s a Truck – all it will know is that it’s of type MyModel – using generics lets me avoid the cast to Truck in the MyClient code:

public class MyClient {

public void doSomething() {
    Map<String, Class<? extends MyModel>> oldMap = new HashMap<String, Class<? extends MyModel>>();
    oldMap.put ("car", Car.class);
    oldMap.put("truck", Truck.class);

    FooBar f = new FooBar();
    Map<Class<? extends MyModel>, List<? extends MyModel>> newMap = f
            .convertToModelList(oldMap);

            // I'm trying to avoid this cast
            List<Truck> trucks = (List<Truck>)newMap.get(Truck.class);
}
}
  • 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-05-29T23:06:08+00:00Added an answer on May 29, 2026 at 11:06 pm

    Although the common superclass of Car and Truck is MyModel, the common superclass of List<Car> and List<Truck> is Object. The map should be of type Map<Class<? extends MyModel>, Object>>. You better wrap the map with some invariant checks and casting instead of getting info directly from the map.

    This may be helpful too: Heterogeneous container to store genericly typed objects in Java

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

Sidebar

Related Questions

Trying to determine if it is possible to bind the SelectedValue of a ComboBox
I am trying to determine if it is possible to get a list of
I am trying to determine if a phone is located in this polygon using
I am trying to determine the correct RegEx syntax to perform the following. I
I am trying to determine if it is possible to add a concatenated string
I've just started trying to use Solr, and already I think that I'm attempting
I am trying to determine how I want to tackle this... this is all
I have nopt seeing anyone else trying to do this. It is completely possible
I am trying to figure the best logic to determine if the user of
I'm trying to determine if SSO is what I want to use in my

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.