Inverse of Supplier in Guava

前端 未结 3 2022
盖世英雄少女心
盖世英雄少女心 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:20

    You have already found the answer. If you just want to visit, you can use filter with a predicate that always returns true; if you are super defensive you can use any predicate and use an or function with an alwaysTrue in the filter itself; just add the or at the end to avoid shortcircuiting.

    The problem is that even though I agree that conceptually Predicate and Consumer are different since a Predicate should be as stateless as possible and not have side effects while a consumer is only about the side effects, in practice the only syntactic difference is that one returns a boolean (that can be ignored) and the other void. If Guava had a Consumer, it would need to either duplicate several of the methods that take a Predicate to also take a Consumer or have Consumer inherit from Predicate.

提交回复
热议问题