[External] : Re: Collection::getAny discussion

Stuart Marks stuart.marks at oracle.com
Wed May 12 01:03:33 UTC 2021



On 5/10/21 3:31 AM, Remi Forax wrote:
> Thinking a little more about conflating "first" and "any".
> I wonder if we have not already cross the Rubicon on that matter,
> 
> If we have a HashSet or any collections and using Stream.findFirst()
>    var collection = new HashSet<>(...);
>    var result = collection.stream().findFirst().orElseThrow();
> 
> We will get the result of collection.iterator().next()
> 
> So adding a default method getFirst() on Collection that returns the result of iterator().next() is pretty coherent, no ?

Not really. Streams have a runtime notion of being ORDERED, and no static type. 
Adding Collection.getFirst() has no similar runtime notion. This proposal is to add 
a static type for ordering/reversibility and corresponding operations for it.

I'd still like to hear about the use cases for getAny or whatever we want to call 
the thing. Are callers interested in the collection having zero-or-one, exactly one, 
zero or more, or one or more elements? Talking about iterator().next() without 
considering the use cases, and their implications for pattern matching, is 
short-sighted.

s'marks


More information about the core-libs-dev mailing list