Collection::getAny discussion
Stephen Colebourne
scolebourne at joda.org
Fri Apr 30 21:15:45 UTC 2021
On Fri, 30 Apr 2021 at 19:50, Stuart Marks <stuart.marks at oracle.com> wrote:
> You're asking for something that's somewhat different, which you called the "find
> the first element when there is only one" problem. Here, there's a precondition that
> the collection have a single element. (It's not clear to me what should happen if
> the collection has zero or more than one element.)
I think any get() or getAny() method on Collection is semantically
equivalent to iterator.next(). I'm not sure there is another viable
option.
> * onlyElement -- if source has 1 element, returns it; throws NSEE if empty, IAE if > 1
> * toOptional -- if source has 0 or 1 elements, returns an Optional; otherwise throws
These should be added to the JDK. They are useful.
Stephen
More information about the core-libs-dev
mailing list