Why doesn't Java 8's Predicate extend Function

前端 未结 4 949
慢半拍i
慢半拍i 2021-02-03 19:14

If I wrote the Predicate interface, I\'d want to encode in the interface the fact that it\'s just a function that returns a primitive boolean, like thi

4条回答
  •  执笔经年
    2021-02-03 19:28

    The method in Predicate returns boolean. The method in Function returns Boolean. They are not the same. Although there is autoboxing, Java methods don't use wrapper classes when primitives would do. Also, there are differences like Boolean can be null while boolean can't.

    It's even more different in the case of Consumer. The method in Consumer has return type void, which means it can implicitly return or return using return;, but the method in Function must return using return null; explicitly.

提交回复
热议问题