Working with immutable collections

Johan Haleby johan.haleby at gmail.com
Mon Nov 19 07:15:19 PST 2012


This would work fine as well, the main point is that I want to avoid
wrapping my statement in Collections.unmodifiableList(..) etc.

/Johan

On Mon, Nov 19, 2012 at 4:07 PM, Gernot Neppert <mcnepp02 at googlemail.com>wrote:

> I'd rather have an "unmodifiable" member function in java.util.Collection,
> together with a "synchronized" function, something like this:
>
> public interface Collection<E> {
>
> /**
>   Returns an unmodifiable view of this Collection.
>   @return an unmodifiable view of this Collection (may be {@code this}
> instance if is already unmodifiable).
> public Collection<E> unmodifiable() default {
>       return Collections.**unmodifiableCollection(this);
>     }
>
> /**
>   Returns a synchronized view of this Collection.
> @param lock the Lock to use.
>   @return a view of this Collection that executes all operations while
> holding a Lock. (may be {@code this} instance if is already synchronized on
> the supplied Lock).
> @throws IllegalArgumentException if this Collection is already
> synchronized on a different Lock.
> public Collection<E> synchronize(Lock lock) default {
>       return Collections.synchronize(this, lock);
>     }
>
> }
>
>
> These should of course be co-variantly overriden in java.util.List,
> java.util.Set etc.!
>
>
>
>
>
>
> Am 19.11.2012 15:39, schrieb Johan Haleby:
>
>> 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