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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:46:48+00:00 2026-06-14T10:46:48+00:00

In Java, if I have the following information: List.class, Map.class, Integer.class , and an

  • 0

In Java, if I have the following information: List.class, Map.class, Integer.class, and an instance foo of some Object. Is it possible to cast foo into List<Map<Integer, Integer>>? I suppose not due to type erasure, but wondering if there is another way to achieve something similar.

EDIT: To provide more context. I’m writing an application that is able to take a query (which is composed of an object and a metric) and outputs the desired metric. An example would be “the sum of the elements in a list”. The client would need to write a provider that registers itself to the app and broadcasts that it is able to provide a certain metric type for some object type (e.g. sum for a list). The app, upon receiving the query will match up the correct provider and invoke the calculate method on it.

Without generics this is simple. The canonical type for any instance of an object is its class. So I would treat the objects in the query as an Object then using its class, find a provider that is able to provide for this instance of the object. Similar thing for metrics.

Now introducing generics…Suppose I define my metric f to be the sum of elements in a list if the elements are Integer and the product if the elements are Double. I would need 2 providers one that provides for List<Integer> and the other for List<Double>. But due to type erasure I cannot use the canonical type class directly. So I store the involved classes (List.class and Integer.class or List.class and Double.class) and match up the provider using tuples of classes.

Hence in the calculate method I need to perform a covariant cast from Object to the appropriate type that the provider handles.

Perhaps I’m taking the wrong approach to the problem?

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

    The closest you’ll get is this:

    List<Map<Integer, Integer>> fooList = List.class.cast(foo);
    

    It will produce a Compiler Warning because of unsafe casting and usage of raw types, but it is actually safe (if you know your Object to contain what it says it does).

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

Sidebar

Related Questions

I'm looking at some Java classes that have the following form: public abstract class
I have following UML class diagram and java coding for each, please tell me
I have following Code package cyclist.project; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException;
In Java I have created the following class which extends ImageView to create a
I have two of the following java classes (listed below) Class BookInfo declares static
I am using GWT/JAVA for development. I have following problem: I want to remove
I have the following java code: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
I have the following Java code: final Future future = exeService.submit( new Runnable() {
I have the following piece of Java code that reads strings from CSV file.
I have the following in a java file (MyRtmpClient.java): import org.apache.mina.common.ByteBuffer; and ByteBuffer is

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.