Working with immutable collections

Brian Goetz brian.goetz at oracle.com
Mon Nov 19 08:06:09 PST 2012


>  > LinkedList implements List and Queue, so when you try to inherit
> unmodifiable() from both List (returns List) and Queue (returns Queue),
> you have to override it with something that returns both a List and a
> Queue.
>
> ...and the top candidate for that "something" would be LinkedList,
> wouldnt' it?

Right.  But, if you were the maintainer of LinkedList, and someone came 
along and said "hey, I know your class was working fine, but now you 
have to implement these two additional methods (which are each probably 
60 lines of (admittedly trivial) code", you'd probably be grumpy at that 
someone.

>  > Unfortunately this means reimplementing Unmodifiable for ad-hoc
> intersections like List&Queue.
>
> OK, I get what you mean, but wouldn't this only prevent you from
> implementing both List and Queue in an anonymous manner?
> Otherwise, similiar to LinkedList, you'd simply have to return the
> implementing class type from such methods.

But you actually have to implement the functionality, and there's no 
class that does it for you (unless you want to call both supers, cache 
the result, and wrap a dynamic proxy around them.)  Which means YOU have 
to re-implement unmodifiableLinkedList.



More information about the lambda-dev mailing list