Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Collections » Builders for immutable collections (*List).
Builders for immutable collections (*List). [message #1783177] Thu, 08 March 2018 20:20 Go to next message
Steve Harris is currently offline Steve HarrisFriend
Messages: 1
Registered: March 2018
Junior Member
Hi all,
I commonly come across cases where I need to mutably build a large list of items within a single method, then "finalize it" as an immutable list to return.

I'd like to be able to do without having to copy the items like toImmutableList() would when called on the mutable lists. The mutable "builder" form of the thing doesn't have to conform to any specific interfaces other than having an "add" method for my purposes - and it would give up it's reference to the data when building the immutable list. Is there any way of doing this currently, or would such a feature be considered as an enhancement?
Re: Builders for immutable collections (*List). [message #1804584 is a reply to message #1783177] Fri, 29 March 2019 03:45 Go to previous message
Donald Raab is currently offline Donald RaabFriend
Messages: 12
Registered: March 2016
Junior Member
Hi Steve,

Sorry for the delay in responding to your question. There is no way to do this today, in terms of returning the ImmutableList interface after building up a MutableList without doing a final copy. However, you can use asUnmodifiable() as follows, which will simply adapt the MutableList with an unmodifiable adapter.

MutableList<Integer> integers = Lists.mutable.with(1, 2, 3, 4, 5).asUnmodifiable();

If you don't want to expose the mutating methods, you can additionally return ListIterable<Integer>.

ListIterable<Integer> integers = Lists.mutable.with(1, 2, 3, 4, 5).asUnmodifiable();

Hope that helps.


[Updated on: Fri, 29 March 2019 04:34]

Report message to a moderator

Previous Topic:Eclipse Collections p2 update site
Next Topic:Ten reasons to use Eclipse Collections
Goto Forum:

Current Time: Sat Sep 30 16:59:37 GMT 2023

Powered by FUDForum. Page generated in 0.02128 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top