Is adapting a no-arg method into a Consumer bad form?

蓝咒 提交于 2019-12-07 18:48:48

问题


Someone raised a question on another SO Answer about whether it is bad practice or inefficient to do this:

Optional<User> user = ...
user.ifPresent(u -> doSomethingWithoutUser());

instead of

if (user.isPresent()) doSomethingWithoutUser();

Specifically, the fact that we're adapting a zero-arg method into a Consumer<User> which ignores its parameter u.

  • As this isn't a Stream non-terminal operation, the fact doSomethingWithoutUser() likely has side-effects isn't a concern.
  • I'm not bothered about the specifics of this one-line Optional example, it could the result of a long chain of functional/Stream calls that simply feels natural to finish inline with the lambda call.

来源:https://stackoverflow.com/questions/58734350/is-adapting-a-no-arg-method-into-a-consumer-bad-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!