Working with immutable collections

Johan Haleby johan.haleby at gmail.com
Mon Nov 19 06:39:30 PST 2012


Hi,

I'm sorry if this has been discussed before but here goes:

Let's say I have an immutable list called "xList" that I want to filter and
return the result as a new *immutable* list. I could do something like this:

Collections.unmodifiableList(xList.filter(obj -> obj.something()).into(new
ArrayList<X>())));

How ever I think this is quite verbose and doesn't follow the fluent style
of the API. What I would like to have is something a long these lines:

xList.filter(obj -> obj.something()).intoImmutable(new ArrayList<X>()));

"intoImmutable" could then do "Collections.unmodifiableList(..)" for me so
I don't need to wrap all statements where I want to return an immutable
collection.

What are your thoughts on this?

Regards,
/Johan


More information about the lambda-dev mailing list