Inverse of Supplier in Guava

前端 未结 3 2027
盖世英雄少女心
盖世英雄少女心 2021-02-19 02:54

I\'m looking for the inverse of Supplier in Guava. I hoped it would be called Consumer – nope – or Sink – exists, but is for pri

3条回答
  •  爱一瞬间的悲伤
    2021-02-19 03:28

    Your alternatives are:

    • Java 8 introduces a Consumer interface which you can compose.
    • Xtend's standard library contains Procedures.
    • Scala has Function*; if a function's return type is Unit, it is considered a side effect.

    In all of these languages, you can use functional interfaces conveniently, so you could also use e.g. Functional Java's Effect.

    Otherwise, you better rely on existing language constructs for performing side effects, e.g. the built-in for loop. Java < 8 inflicts tremendous syntactic overhead when using lambdas. See this question and this discussion.

提交回复
热议问题