-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please add toOptional and onlyElement collectors #188
Comments
We have |
Does |
... just checked the code. It does terminate the stream processing. Nice! |
Thanks for your comments. While indeed Yes, there are cases when we want to get an exception (to catch it later or to terminate the thread) if the condition does not hold. For example, when I select a row from DB using primary key, I want to use I understand that I can code these thing myself using the existing facilities, but since the main goal of StreamEx (as I see it) is to make development using Java streams more pleasant, I see no problem in adding more helpers to make code shorter and more expressive. In my opinion is it better to have rich set of collectors for every occasion rather than provide a minimal required set. Thanks. |
Hello,
Please add two more collectors:
onlyElement
if streams has exactly one element, return it; otherwise throw an exception
toOptional
return Optional.empty is stream is empty, Optional.of(elem) if streams has exactly one element, and throw an exception otherwise
Names taken from Guava's MoreCollectors.
Alternative good choice for naming is single / singleO.
Thanks!
The text was updated successfully, but these errors were encountered: