----- Mail original ----- > De: "Stephen Colebourne" <scolebou...@joda.org> > À: "core-libs-dev" <core-libs-dev@openjdk.java.net> > Envoyé: Samedi 24 Avril 2021 00:14:51 > Objet: Re: New Collections interface - Sized
> On Fri, 23 Apr 2021 at 23:07, Brian Goetz <brian.go...@oracle.com> wrote: >> >> Is there a compelling example of where this would be used by clients? >> > Here are some examples: >> > https://stackoverflow.com/questions/10988634/java-global-isempty-method >> Without passing judgment on the sort of dynamically typed programs that >> need a method like this > > The other example is better as it benefits from declaring an API that > only accepts instances of `Sized` and does not need to get the > contents. > >> But again, if you are treating these things as containers, then a Sized >> doesn't get you very far, because if you conclude the thing isn't empty, >> you're going to want to get stuff out, and Sized has no methods for >> that. So presumably there's some companion to Sized for accessing >> elements by index: >> >> interface HasStuff<T> extends Sized { >> T get(int index); >> } > > I don't think there has to be. the more useful interface would be this > one, but to date there has been strong resistance in unifying the > Collection and Map interfaces: > > interface Stuff<T> extends Sized { > int size(); > int isEmpty(); > int isNotEmpty(); > Iterator<t> iterator(); > } This is basically Spliterator, an iterator + a size, with the iterator is "push" instead of "pull" because it's more efficient. In details a Spliterator is either - an Iterable (with no SIZED characteristic) - an Iterable + size (if SIZED and estimateSize() != Long.MAX_VALUE) - an Iterable + comparator (if SORTED and comparator != null) - an Iterable + split (if trySplit != null) and all combinations of the above. > > Stephen Rémi