----- Mail original -----
> De: "Stuart Marks" <stuart.ma...@oracle.com>
> À: "Remi Forax" <fo...@univ-mlv.fr>
> Cc: "core-libs-dev" <core-libs-dev@openjdk.java.net>
> Envoyé: Mercredi 5 Mai 2021 02:00:03
> Objet: Re: [External] : Re: ReversibleCollection proposal

> On 5/1/21 5:57 AM, fo...@univ-mlv.fr wrote:
>> I suppose the performance issue comes from the fact that traversing a
>> LinkedHahSet is slow because it's a linked list ?
>> 
>> You can replace a LinkedHashSet by a List + Set, the List keeps the values in
>> order, the Set avoids duplicates.
>> 
>> Using a Stream, it becomes
>>    Stream.of(getItemsFromSomeplace(), getItemsFromAnotherPlace(),
>>    getItemsFromSomeplaceElse())
>>     .flatMap(List::stream)
>>     .distinct()              // use a Set internally
>>     .collect(toList());
> 
> The problem with any example is that simplifying assumptions are necessary for
> showing the example, but those assumptions enable it to be easily picked 
> apart.
> Of
> course, the example isn't just a particular example; it is a *template* for a
> whole
> space of possible examples. Consider the possibility that the items processing
> client needs to do some intermediate processing on the first group of items
> before
> adding the other groups. This might not be possible to do using streams. Use
> your
> imagination.
> 
>> I think there are maybe some scenarios where ReversibleCollection can be 
>> useful,
>> but they are rare, to the point where when there is a good scenario for it
>> people will not recognize it because ReversibleCollection will not be part of
>> their muscle memory.
> 
> I'm certain that uses of RC/RS will be rare in the beginning, because they 
> will
> be
> new, and people won't be familar with them. And then there will the people who
> say
> "I can't use them because I'm stuck on JDK 11 / 8 / 7 / 6 ...." It was the 
> same
> thing with lambdas and streams in Java 8, with List.of() etc in Java 9, 
> records
> in
> Java 16, etc. This wasn't an argument not to add them, and it isn't an 
> argument
> not
> to add RC/RS.

All the changes you are listing here are "client side" changes, the ones that 
can be adopted quickly because they do not require to change the API side of 
any libraries.
ReversibleCollection is an API side change, like generics is, those changes 
have a far higher cost because you have to wait your library dependencies to be 
updated.
On the Valhalla list, we have discussed several times about how to alleviate 
those API side change cost using automatic bridging or methods forwarding, even 
for Valhalla, we are currently moving in a state where those mechanisms are not 
needed. 

> 
>> There is a real value to add methods like
>> descendingSet/descendingList()/getFirst/getLast on existing collections, but 
>> we
>> don't need a new interface (or two) for that.
> 
> It depends on what you mean by "need". Sure, we could get away without this;
> after
> all, we've survived the past twenty years without it, so we could probably
> survive
> the next twenty years as well.
> 
> It would indeed be useful to add various methods to List, Deque, SortedSet, 
> and
> LinkedHashSet to provide a full set of methods on all of them. And it would 
> also
> be
> nice to have those methods be similar to one another. An interface helps with
> that,
> but I agree, that's not really the reason to have an interface though.
> 
> The reversed-view concept could also be added individually to the different
> places.
> A reverse-ordered List is a List, a reverse-ordered Deque is a Deque, a
> reverse-ordered SortedSet is a SortedSet, and a reverse-ordered LinkedHashSet 
> is
> a
> ... ? And what is the type of the keySet of a LinkedHashMap, that enables one 
> to
> (say) get the last element?

see below

> 
> After working with a system like this for a while, it begins to emerge that
> there is
> an abstraction missing from the collections framework, something like an
> "ordered
> collection". People have been missing this for quite a long time. The most
> recent
> example (which this proposal builds on) is Tagir's proposal from a year ago. 
> And
> it's been asked about several times before that. ReversibleCollection fills in
> that
> missing abstraction.

The abstraction already exists but it's not defined in term of interface 
because it's an implementation decision and those are cleanly separated in the 
current Collection design.

Let take a step back, the collection API defines basic data structure 
operations in term of interfaces like List, Deque, Set, etc those interfaces 
are decoupled from implementation capabilities like mutable, nullable, ordered 
and checked.

Depending on the implementation capabilities, the interfaces method 
implementation may throw an exception, non-mutable implementations use 
UnsupportedOperationException, non-nullable implementations use NPE and checked 
implementations use CCE. 

So what is missing is methods on Collection interfaces that require the 
collection implementation to be ordered like descendingList(), getFirst(), etc.
Those methods that may throw a specific exception if the implementation is not 
ordered, not UnsupportedOperationException but a new one like 
NotOrderedException.

So to answer to your question about LinkedHashSet, the reverse-ordered 
LinkedHashSet is a Set with a method descendingSet() that do not throw 
NotOrderedException like any Set with an order.

To summarize, if we introduce ReversibleCollection, we should also introduce 
ImmutableCollection, NonNullableCollection and CheckedCollection.
I think it's better to consider the fact that being ordered as a capability 
(hint: this is already what the Spliterator API does) and not as a specific 
interface.

> 
> s'marks

Rémi

Reply via email to