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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:43:36+00:00 2026-06-12T05:43:36+00:00

My (distilled) Scenario: Let’s say I have an interface called Interface , and a

  • 0

My (distilled) Scenario:

Let’s say I have an interface called Interface, and a class Foo implements Interface.

I also have a generic iterable “Result” class which among other things delegates to a collection to do the iterable stuff. The Result class does other stuff not relevant to the question. So:

class Result<E> implements Iterable<E> {

    private Collection<E> delegate;

    public Result(Collection<E> delegate) {
        this.delegate = delegate;
    }

    public Iterator<E> iterator() {
        return delegate.iterator();
    }

    // ... other irrelevant stuff.
}

The collection passed into Result can potentially be huge, while users of the Result object are likely to stop iterating after a few elements (but I have no control over how many).

Then, I have another class, let’s call it Query, which internally holds a collection of Foos, but needs to be able to return Result. It currently looks like this:

class Query {
    private Collection<Foo> data;

    public Result<Interface> getAllData() {
        return new Result<Interface>(new ArrayList<Interface>(data));
    }

    // EDIT: Not all Result objects are of Interfaces.
    public Result<SomeUnrelatedInterface> getSomeOtherData() {
        return ...;
    }
}

So getAllData takes a copy of the data (as an ArrayList<Interface>), and passes that to the new Result for delegation. This is not ideal for me, as that collection is probably huge, and the receiver of the result probably only needs the first few results.

Now for the question:

Can anyone think of way of changing things such that I do not need to take that copy of the concrete collection?

Ideally, I’d like to have Results constructor be public Result(Collection<? extends E> delegate) (like most Collections do), and have it somehow adapt that collection to Collection<E>, but I have not been able to figure out what that might look like.

  • 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-12T05:43:38+00:00Added an answer on June 12, 2026 at 5:43 am

    As you know, the danger in turning a Collection<? extends E> into a Collection<E> is that now the caller can put any E into it. So you’d have to rule that out and you’d be fine. Collections.unmodifiableCollection(Collection) does just that and it will even present the result as a Collection<E>.

    Edit:

    Collection<? extends Interface> foos = new ArrayList<Foo>();
    Collection<Interface> adapted = Collections.unmodifiableCollection(foos);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a (common) situation where I am currently returning a result that is
I have been debugging a query and have distilled it down to a simple
I have a model that contains a dictionary property. (this has been distilled from
I'm trying to create a view, and have distilled the problem down to the
I have a problem in my code which I have distilled down to the
EDIT: I have somewhat distilled the question. mongo_documents = mongo_collection.find({medicalObjectId: 269}) print \n\n for
I've distilled my problem down to this code snippet - but it is a
In section 4.5 Combining Sharding and Replication of the NoSQL Distilled book, the following
I am inserting metadata into postscript files with a program, to be distilled to
I've distilled an equation down to this: speed = ( ( rear_wheel_speed_a + front_wheel_speed_a

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.