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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:50:07+00:00 2026-06-15T18:50:07+00:00

I want to implement a immutable class that is Iterable in Java. I do

  • 0

I want to implement a immutable class that is Iterable in Java. I do it the following way:

public final class MyIterable implements Iterable<Integer> {

    private final Collection<Integer> items;

    public MyIterable(Collection<Integer> items) {
        this.items = Collections.unmodifiableCollection(new LinkedList<Integer>(items));
    }

    @Override
    public Iterator<Integer> iterator() {
        return items.iterator();
    }
}

This is all good, I cannot change anything in my instances. However, my class still exposes an API that indicates that its internals could be modified through the remove() method on the iterator:

MyIterable myIterable = new MyIterable(Arrays.asList(1, 2, 3));
for (Iterator<Integer> it = myIterable.iterator(); it.hasNext();) {
    it.remove(); // I do not want to expose this even 
                 // though I know it will throw an error at runtime.
    it.next();
}

Is there some way that I can avoid exposing this remove method and hence have my class exposing a truly immutable API? Ideal would be implementing something like ReadOnlyIterable instead, but something like that does not seem to be available in Java.

  • 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-15T18:50:08+00:00Added an answer on June 15, 2026 at 6:50 pm

    It would seem to indicate it by virtue of its method signatures, but those methods are specifically documented to say “No, we do not supply those behaviors!”

    In order to carry this through, you would have to also document that you follow identical behaviors to UnmodifiableList.

    It is the responsibility of developers to know what the libraries they use do, and the responsibility of library creators to make that information available.

    Bottom line is, Iterable is baked into the language (the for(a:b) loop) and while you could create your own interface that is ReadOnlyIterable it simply won’t be as robust.

    The problem with this approach is that you sacrifice compile-time integrity. In other words, someone can compile code that uses the remove() method, and they won’t find out until runtime that it doesn’t work. This means if they happen to not test properly, you won’t find out until it’s in production that the method shouldn’t be used.

    You could perhaps use annotations and warnings to mitigate this – if they listen to warnings, or use an IDE that tells them about warnings, they’ll find out much earlier. But you can’t rely on users to do the right thing. That’s why you have to throw an exception instead of doing nothing.

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

Sidebar

Related Questions

given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Is there anything in Java that implements something like the following interface MSet<T> extends
I wrote small service class and I want implement something like that : service
I have class, that contains different types and implements Iterable<Object> interface. But it is
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
If I have a class that needs to implement an interface but one or
This fluent like class is not strictly immutable because the fields are not final,
everyone! My english is poor and sorry fot that. I want implement a function
I want to implement the replication and fault tolerance in CORBA using Java. I
I want to implement file upload in RESTful way using openrasta but not able

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.