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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:31:38+00:00 2026-05-22T11:31:38+00:00

how can i write this code in java? public class ComponentsManager { private List<IComponent>

  • 0

how can i write this code in java?

public class ComponentsManager 
    {
        private List<IComponent> list = new ArrayList<IComponent>();

        public <U extends IComponent> U GetComponent() {

            for (IComponent component : list) {

                if(component instanceof U)
                {
                    return component;
                }
            }
        }
}

but i cant perform instanceof on generic types. how should i do it?
thanks.

  • 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-05-22T11:31:39+00:00Added an answer on May 22, 2026 at 11:31 am

    Basically you can’t do that due to type erasure. The normal workaround is to pass a Class object as a parameter; e.g.

        public <U extends IComponent> U GetComponent(Class<U> clazz) {
            for (IComponent component : list) {
                if (clazz.isInstance(component)) {
                    return clazz.cast(component);
                }
            }
        }
    

    You could also use if (clazz.equals(component.getClass())) { ... but that does an exact type match … which is not what the instanceof operator does. The instanceof operator and the Class.instanceOf method both test to see if the value’s type is assignment compatible.

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

Sidebar

Related Questions

Eg. can I write something like this code: public void InactiveCustomers(IEnumerable<Guid> customerIDs) { //...
I want to write this piece of code : @Stateless public class MyEjb {
In Java you can define a new class inline using anonymous inner classes. This
How can I write this code more cleanly/concisely? /// <summary> /// Creates a set
I have this code to update users data, but I can't write the for
If I'm using a Hashtable , I can write code like this: object item
Can someone write some sample code to explain this concept? I know what a
Using GWT I have a Java class: public class Pojo { private String name;
Let us have the following code: public class TestGenerics { static <T> void mix(ArrayList<T>
How can I make this java generic cast ? public interface IField { }

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.