Default Methods


Idioms and Techniques

Design Rationale

Advanced Questions

Are there other “constant” lambdas like Function.identity()?

No, but for many such “constant” functions it’s easy to write your own lambda expression. In fact, one can write x -> x instead of Function.identity() and in some cases it’s clearer. Here is a list of common constant functions and how they can be written using lambda.

Predicate that always returns true or false:

x -> true
x -> false

Runnable that does nothing:

() -> { }

Consumer that does nothing:

x -> { }

Supplier that returns a constant k. This could be a value that’s captured from the lexical environment, or it could be a literal:

() -> k

Function that returns a constant k:

x -> k

Identity function, the same as Function.identity():

x -> x

Function taking two arguments that returns the first or second:

(a, b) -> a
(a, b) -> b