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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:47:47+00:00 2026-06-02T15:47:47+00:00

I need to create generic list that can act as List<ClassA> or List<ClassB> because

  • 0

I need to create generic list that can act as List<ClassA> or List<ClassB> because I have following situation.

if(e instanceof A)
return A;
else
return B

But I want to populate one List Result which can act as either List<ClassA> Result or List<ClassB> Result, am sure we can use generics here but i am not sure how to use it?

Update

All i want to do is that runtime, my List should be populated with proper type, either class A or class B,

if()
{
    //data is present in List<ClassA> classAList = new ArrayList<ClassA>();
    //return classAList
}
else
{
  //return List<ClassB> classBList = new ArrayList<ClassB>();
}

Hope this helps.

  • 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-02T15:47:50+00:00Added an answer on June 2, 2026 at 3:47 pm

    So if I understand you correctly, you want to be able to construct a list of ClassA or a list of ClassB depending on if a given object is ClassA or ClassB.

    public List<? extends Object> getList(Object e) {
        List<? extends Object> list = new ArrayList<? extends Object>();
        if(e instanceof ClassA) {
            List<ClassA> listA = new ArrayList<ClassA>();
            // Populate list somehow
            list = listA;
        } else if (e instanceof ClassB) {
            List<ClassB> listB = new ArrayList<ClassB>();
            // Populate list somehow
            list = listB;
        }
        return list;
    }
    

    Unfortunately, the return type must allow for either type (you can alternatively create two methods with different return types, but they must have different names). However, this allows you to use listA and listB as their own types before assigning it to the more generic list.

    However this is a violation of the SOLID principal, so an alternative might be:

    public abstract class Root<T> {
        public abstract List<T> getList();
    }
    
    public class ClassAList extends Root<ClassA> {
        @Override
        public List<ClassA> getList()  {
            List<ClassA> list = new ArrayList<ClassA>();
            // Populate list somehow
            return list;
        }
    }
    
    public class ClassBList extends Root<ClassB> {
        @Override
        public List<ClassB> getList()  {
            List<ClassB> list = new ArrayList<ClassB>();
            // Populate list somehow
            return list;
        }
    }
    

    Hope that helps!

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

Sidebar

Related Questions

I need to create an app that one of the feature will have an
I know I can create a generic method that I can call from other
I am trying to create a function that accepts a generic List<T> and iterates
I have a few generic classes that implement a common non-generic interface. I create
I have a method that uses a list which I need to pass into
How can I use reflection to create a generic List with a custom class
I have a following problem: I need to store generic objects with different type
I want to create generic function that will need only parameter as Stored procedure
I need to create at runtime instances of a class that uses generics, like
i need create an email list sending to many emails. what is best solution

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.