Thursday 12 September 2019

Stream to Optional

Was looking for a function that returns an Optional containing a value if it was the only value in the stream (size of stream is 1) and an empty Optional in every other case.

I came up with the following:

The limit that is put upon it, might make matters really performant, but perhaps not in the case of parallel streams.

See the javadoc of limit() on why this is.

No comments:

Post a Comment