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

  • Home
  • SEARCH
  • 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 8616855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:43:04+00:00 2026-06-12T05:43:04+00:00

Possible Duplicate: Generic type of local variable at runtime I’m new to Java generics,

  • 0

Possible Duplicate:
Generic type of local variable at runtime

I’m new to Java generics, and coming from a .NET world, I’m used to being able to write a method like this:

public void genericMethod<T>(T genericObject)
{
    if (genericObject is IList<String>)
    {
        //Do something...
    }            
}

The method accepts an object of a generic type, and checks whether that object implements a specific version of the generic interface IList<>, in this case, IList<String>.

Now, in Java, I’m able to do this:

public <T> void genericMethod(T genericObject)
{
    if (genericObject instanceof Set<?>)
    {
       //Do something...
    }
}

BUT

Java does not let me do if (genericObject instanceof Set<String>)

From what I know, because of type erasure, normally in Java this would be taken care of by a class object, and we would do something like the following:

public <T> void genericMethod(T genericObject)
{
    Class<OurTestingType> testClass = OurTestingType.class;
    if (genericObject.getClass() == testClass)
    {
       //Do something...
    }
}

but since the type I’m checking for is a generic interface, you can’t do this:

Class<Set<String>> testClass = Set<String>.class

So, how, in Java, do I check if a generic object implements the specific type of Set<String>?

  • 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-12T05:43:05+00:00Added an answer on June 12, 2026 at 5:43 am

    Java implements erasure, so there’s no way to tell on runtime if genericObject is an instance of Set<String> or not. The only way to guarantee this is to use bounds on your generics, or check all elements in the set.

    Compile-time Generic Bounds

    Using bounds checking, which will be checked at compile-time:

    public <T extends SomeInterface> void genericMethod(Set<? extends T> tSet) {
        // Do something with tSet here
    }
    

    Java 8

    We can use streams in Java 8 to do this natively in a single line:

    public <T> void genericMethod(T t) {
        if (t instanceof Set<?>) {
            Set<?> set = (Set<?>) t;
            if (set.stream().allMatch(String.class:isInstance)) {
                Set<String> strs = (Set<String>) set;
                // Do something with strs here
            }
        }
    }
    

    Java 7 and older

    With Java 7 and older, we need to use iteration and type checking:

    public <T> void genericMethod(T t) {
        Set<String> strs = new HashSet<String>();
        Set<?> tAsSet;
        if (t instanceof Set<?>) {
            tAsSet = (Set<?>) t;
            for (Object obj : tAsSet) {
                if (obj instanceof String) {
                    strs.add((String) obj);
                }
            }
            // Do something with strs here
        } else {
            // Throw an exception or log a warning or something.
        }
    }
    

    Guava

    As per Mark Peters’ comment below, Guava also has methods that do this for you if you can add it to your project:

    public <T> void genericMethod(T t) {
        if (t instanceof Set<?>) {
            Set<?> set = (Set<?>) t;
            if (Iterables.all(set, Predicates.instanceOf(String.class))) {
                Set<String> strs = (Set<String>) set;
                // Do something with strs here
            }
        }
    }
    

    The statement, Iterables.all(set, Predicates.instanceOf(String.class)) is essentially the same thing as set instanceof Set<String>.

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

Sidebar

Related Questions

Possible Duplicate: get type of a generic parameter in java with reflection Java Generics
Possible Duplicate: Determining the Type for a generic method parameter at runtime Generics in
Possible Duplicate: Create instance of generic type in Java? Java how to: Generic Array
Possible Duplicate: Type-parameterized field of a generic class becomes invisible after upgrading to Java
Possible Duplicate: Get generic type of java.util.List I have a Map and I want
Possible Duplicate: Reflection - Getting the generic parameters from a System.Type instance Get actual
Possible Duplicate: Why won't this generic java code compile? Given the following code: import
Possible Duplicate: How to determine the class of a generic type? How to obtain
Possible Duplicate: Java how to: Generic Array creation Error generic array creation I have
Possible Duplicate: How to determine the class of a generic type? I have 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.