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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:45:22+00:00 2026-05-23T17:45:22+00:00

I have two classes named BaseCatalog and City. BaseCatalog is the parent class of

  • 0

I have two classes named “BaseCatalog” and “City”. “BaseCatalog” is the parent class of “City”, in other words, “City” extends “BaseCatalog” class.

I have a method called “getBaseCatalogObjects” which takes one parameter catalogType and
returns a list of BaseCatalog objects. According to the given type, the type of the List may differ but it always contain objects that extends the main BaseCatalog class.

Because this method is a generic method, it always produces a List containing BaseCatalog objects. However in this case, I am sure each BaseCatalog instance in the list is also a City object:

List<BaseCatalog> baseCatalogObjects = getBaseCatalogObjects(CatalogType.CITY);

What I want to do is to convert this “baseCatalogObjects” list into a list called “cityObjects” which is something like:

List<City> cityObjects = (List<City>) baseCatalogObjects;

Is it possible or is there any efficient way to do such a conversion like this without creating a new java.util.List instance and iterating over the list “baseCatalogobjects”?

  • 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-23T17:45:23+00:00Added an answer on May 23, 2026 at 5:45 pm

    As suggested by Peter you can change your method signature to something as follows,

    public <T extends BaseCatalog> List<T> getBaseCatalogObjects(Class<T> clazz, CatalogType type){}
    

    And then you can cast your list in the return statement like,

    (List<T>) list;
    

    Hope this helps!

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

Sidebar

Related Questions

I have a super-class named ClassA and two sub-classes Class1 and Class2. I have
I have two classes, named Post and Question. Question is defined as: public class
I have two classes A and B. Class A contains a UIView named myView
I have two classes named ROAD and PATH public class ROAD { public string
Summery of problem:- I have classes named AttachPicture which extends the ListActivity and another
I am working on a business problem in C#.NET. I have two classes, named
I have two classes, Portfolio, and PortfolioImage. class PortfolioImage(models.Model): portfolio = models.ForeignKey('Portfolio', related_name='images') ...
I have two classes, and want to include a static instance of one class
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two classes that each need an instance of each other to function.

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.