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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:02:56+00:00 2026-06-16T23:02:56+00:00

Possible Duplicate: How do you cast a List of objects from one type to

  • 0

Possible Duplicate:
How do you cast a List of objects from one type to another in Java?

Searched the internet a little, and found no nice way of doing it… My solution now is:

public class A {}
List<Object> obj = new ArrayList<Object>();
obj.add(new A());
// Ugly solution here:
List<A> a = (List<A>) (List) obj;

But this is quite ugly and gets a warning. No “official” way of doing this?

EDIT: To the guys who closed this: I was aware of the solution posted in How do you cast a List of objects from one type to another in Java? It is the same as the one I posted in my question (just adding the <?> after the first cast does exactly the same) I Was looking for something more “clean”. In the direction of using the Class<?> clazz = listobj.get(0).getClass way of getting the class and casting to the correct class at runtime (but no idea if something like that works… Eclipse doesn’t seem to like it anyway…)

  • 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-16T23:02:57+00:00Added an answer on June 16, 2026 at 11:02 pm
    List<Object> obj = new ArrayList<Object>();
    obj.add(new A());
    

    It is not the right way to write code. Basically you are creating a generic List and adding Object to it and it type unsafe and keep any Object type.

    List<Object> obj = new ArrayList<Object>();
    obj.add(new A());
    obj.add(new String("str"));
    obj.add(1);
    

    It is recommended to create type-safe List like List<A> obj = new ArrayList<A>();

    you can do this in such a way –

    public <T>List<T> castCollection(List srcList, Class<T> clas){
        List<T> list =new ArrayList<T>();
        for (Object obj : srcList) {
        if(obj!=null && clas.isAssignableFrom(obj.getClass()))
            list.add(clas.cast(obj));
        }
        return list;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java Generics: Cannot cast List<SubClass> to List<SuperClass>? I want to create function
Possible Duplicate: IEnumerable.Cast<> One can implicitly convert from int to double. Why does Specified
Possible Duplicate: Java 1.6: Creating an array of List<T> How can I initialize this
Possible Duplicate: What happens when you cast from short to byte in C#? Can
Possible Duplicate: Most efficient way to cast List<SubClass> to List<BaseClass> why polymorphism doesn't treat
Possible Duplicate: Is List<Dog> a subclass of List<Animal> ? Why aren't Java's generics implicitly
Possible Duplicate: C# generic list <T> how to get the type of T? I
Possible Duplicate: Is it ok to cast a STL container with Base type to
Possible Duplicate: Implicit cast from char** to const char** Given the following code: void
Possible Duplicate: gcc compile error: cast specifies array type I want to check the

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.