ReversibleCollection proposal

Stephen Colebourne scolebourne at joda.org
Fri Apr 23 09:16:49 UTC 2021


On Fri, 23 Apr 2021 at 07:33, Stuart Marks <stuart.marks at oracle.com> wrote:
> On 4/22/21 8:36 AM, Stephen Colebourne wrote:
> Having these methods on Collection could lead to a situation where calling addFirst
> and then getFirst might result in getFirst returning a different element from what
> was passed to addFirst. This doesn't make any sense for methods that have "first" in
> the name.

FWIW, I'm comfortable with that weirdness. The method could be called
addPreferFirst() but that is a bit of a mouthful.

I'd also note that it is much easier for developers of other
Collection implementations to add a method with a matching name than
it is to implement a new interface. Even libraries maintaining
compatibility with Java 5 could do that.

Your proposal has a similar issue BTW - addFirst() on a SortedSet.
Your proposal is to throw UnsupportedOperationException, but I (like
Remi) think that isn't a good answer when
UnsupportedOperationException in collections is usually about
immutability. It is a natural problem of the domain that adding first
when the implementation is sorted is going to be confusing. I am
simply suggesting embracing the weirdness, rather than trying to
exception your way out of it.

Stephen


More information about the core-libs-dev mailing list