Is there a class like Optional but for non-optionals?

Yes, there is such an API. You may replace

Optional.of(vegetables).map(prepare).ifPresent(putOnPlate);

with

Stream.of(vegetables).map(prepare).forEach(putOnPlate);

now having to live with the fact that the single-element Stream is a special case of the stream of arbitrary elements (including the possible empty stream).

But you can handle all mandatory elements at once

Stream.of(mandatory1, mandatory2, mandatory3 /* etc */).map(prepare).forEach(putOnPlate);

It would be even possible to incorporate the optional elements, but it will not be as convenient as it should be, as Optional.stream() will be introduced not until Java 9.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)