On Fri, 30 Apr 2021 at 19:50, Stuart Marks <stuart.ma...@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