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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:09:33+00:00 2026-05-12T16:09:33+00:00

I have a List that is guaranteed to contain just one type object. This

  • 0

I have a List that is guaranteed to contain just one type object. This is created by some underlying code in a library that I cannot update. I want to create a List<ObjectType> based on the incoming List object so that my calling code is talking to List<ObjectType>.

What’s the best way to convert the List (or any other object collection) to a List<ObjectType>.

  • 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-12T16:09:34+00:00Added an answer on May 12, 2026 at 4:09 pm

    When inter-operating with legacy code that doesn’t specify type parameters for generic types, use a wildcard. For example, suppose you are calling a method in an older library that simply returns a raw Collection:

    Collection getItems();
    

    In your code, assign the result to a variable declared with a wildcard:

    Collection<?> items = widget.getItems();
    

    This way, you preserve type safety so you won’t get any warnings.

    The legacy code might specify (in a comment, most likely) what the generic parameters should be. For example:

    /**
     * @return the items, as a Collection of {@link Item} instances.
     */
    Collection getItems();
    

    In this case, you have a choice. You can cast the result to a Collection<Item>, but if you do so, you are relying 100% on the third-party library, and discarding the assurance of Java generic types: that any ClassCastException raised at runtime will occur right at an explicit cast.

    What if you don’t fully trust the third-party library, but still need to produce a Collection<Item>? Then create a new collection, and add the contents after casting them to the expected type. That way, if there is a bug in the library, you find out about it right away, rather than having some code far away and much later mysteriously blow up with a ClassCastException.

    For example:

    Collection<?> tmp = widget.getItems();
    Collection<Item> items = new ArrayList<Item>(tmp.size());
    for (Object o : tmp)
      items.add((Item) o); /* Any type error will be discovered here! */
    

    For a case where the type parameter isn’t known at compile-time, you can use the type-checked collection factories of the Collections class.

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

Sidebar

Related Questions

I have a specialized list that holds items of type IThing : public class
i have a list of regex patterns (stored in a list type) that I
Let's say I have a List object and an iterator for that list. Now
I have a list that looks like (A (B (C D)) (E (F))) which
I have a linked list that I want to sort part of, eg: std::sort(someIterator,
I have a dropdown list that stores name/value pairs. The dropdown appears in each
I have a list wrapper that maintains two Tstringlists and a TClassList I need
I have a list view that is periodically updated (every 60 seconds). It was
i have a list of products that are being displayed in particular order. store
We have an auto-complete list that's populated when an you send an email to

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.