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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:56:59+00:00 2026-06-05T19:56:59+00:00

I have some parts in my code that look like this: A, B and

  • 0

I have some parts in my code that look like this:

A, B and C extend D

public ArrayList<A> getA() {
    ArrayList<A> allElements = new ArrayList<A>();
    for (D el : listOfDs) {
        if (el instanceof A) {
            allElements.add((A) el);
        }
    }
    return allElements;
}

public ArrayList<B> getB() {
    ArrayList<B> allElements = new ArrayList<B>();
    for (D el : listOfDs) {
        if (el instanceof B) {
            allElements.add((B) el);
        }
    }
    return allElements;
}

public ArrayList<C> getC() {
    ArrayList<C> allElements = new ArrayList<C>();
    for (D el : listOfDs) {
        if (el instanceof C) {
            allElements.add((C) el);
        }
    }
    return allElements;
}

I would like to combine all of them to one method like this:

public <T> ArrayList<T> get() {
    ArrayList<T> allElements = new ArrayList<T>();
    for (D el : listOfDs) {
        if (el instanceof T) {
            allElements.add((T) el);
        }
    }
    return allElements;
}

Is this possible in Java?

At the moment I get

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

and

Type safety: Unchecked cast from Node to T

Then I’ve tried this:

@SuppressWarnings("unchecked")
public <T> ArrayList<T> get(Class<T> clazz) {
    ArrayList<T> allElements = new ArrayList<T>();
    for(D o : listOfDs) {
        if (o.getClass() == clazz) {
            allElements.add((T) o);
        }
    }
    return allElements;
}

It doesn’t throw any errors, but how can I call it? This did not work:

get(A);
  • 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-05T19:57:01+00:00Added an answer on June 5, 2026 at 7:57 pm

    You could use Iterables.filter from Guava.

    Example usage:

    Iterable<X> xs = Iterables.filter(someIterable, X.class);
    

    Since it’s an open source library, you can check out the source to find what you are doing wrong.

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

Sidebar

Related Questions

I have some third-party Javascript that has statements like this: FOO = function() {
So, given that I have some type of data structure like this within a
I have some code that at one part will get executed a lot, and
There might be similar questions but I still have some parts that I couldn't
i have this letter where in some parts will be filled-up by the user.
first I want to say that I hope this doesn't look like I am
I have a huge file of code with many lines like this: Enterprise::TextMessageBox::Show(String::Format(SВъзникнал е
I'm writing some code that uses a Tree (a regular tree that can have
I have some existing code that uses an enum instead of a function pointer
I need to extract requests from a log file that look like this :

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.