[External] : Re: ReversibleCollection proposal
Stuart Marks
stuart.marks at oracle.com
Wed May 5 00:00:03 UTC 2021
On 5/1/21 5:57 AM, forax at 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 wan't an argument not to add them, and it isn't an argument not
to add RC/RS.
> 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?
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.
s'marks
More information about the core-libs-dev
mailing list