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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:59:17+00:00 2026-06-17T07:59:17+00:00

Possible Duplicate: Java: Instanceof and Generics I am trying to write a function which

  • 0

Possible Duplicate:
Java: Instanceof and Generics

I am trying to write a function which cast a generic List to specific type List. Find the code below

public <T>List<T> castCollection(List srcList, Class<T> clas){
    List<T> list =new ArrayList<T>();
    for (Object obj : srcList) {
       if(obj instanceof T){
            ...
       }
    }
    return list;
}

But obj instanceof T showing a compilation error –

Cannot perform instanceof check against type parameter T. Use instead its erasure Object >instead since further generic type information will be erased at runtime.

any clarification or way to get the desired result?

Thanks in advance. 🙂

  • 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-17T07:59:18+00:00Added an answer on June 17, 2026 at 7:59 am

    You cannot do it this way. Fortunately, you already have a Class<T> argument so instead do

    myClass.isAssignableFrom(obj.getClass())
    

    This will return true if obj is of class myClass or subclass.

    As @ILMTitan pointed out (thanks), you will need to check for obj == null to avoid a potential NullPointerException, or use myClass.isInstance(obj) instead. Either does what you need.

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

Sidebar

Related Questions

Possible Duplicate: Generic type of local variable at runtime I'm new to Java generics,
Possible Duplicate: Get generic type of java.util.List I have a Map and I want
Possible Duplicate: Create instance of generic type in Java? Java how to: Generic Array
Possible Duplicate: Create instance of generic type in Java? I got these classes public
Possible Duplicate: Java: Always override equals? should I override equals function for any class
Possible Duplicate: Java float division precision I'm trying to fix a bug in one
Possible Duplicate: How to determine the class of a generic type? How to obtain
Possible Duplicate: Java Generics In Eclipse, I am given warnings for using 'rawtypes' and
Possible Duplicate: Generics in for each loop problem if instance does not have generic
Possible Duplicate: Is it possible to reference a nested generic parameter in java? A

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.