- Lamda expression is an anonymous function (i.e without name, return type and access modifiers and having one lamda symbol)
Reasons for Lamda
- One liner code
- Easy to read and understand
So as we understand from above we can reduce small methods into single line.
- If you method
- return true or false use predicate
- take no input and returns 1 output then use supplier (No input hence no bi-supplier)
- take 1 / 2 inputs and does not return anything use consumer/bi-consumer
- take 1/2 inputs and returns a different type as output then use function/bi-function
- Functional Interfaces and its types
- Java 8 Predicate Interfaces (always returns true or false)
- Predicate<T> – 1 object/type as input
- BiPredicate<T,U> – 2 object/type as input
- IntPredicate – 1 Integer as input
- LongPredicate – 1 Long as input
- DoublePredicate – 1 Double as Input
- Java 8 Supplier Interfaces (no input and only output)
- Supplier<T>- 1 object/type as output
- BooleanSupplier – 1 boolean as output
- IntSupplier – 1 Integer as output
- LongSupplier – 1 Long as output
- DoubleSupplier – 1 Double as output
- Java 8 Consumer Interfaces (only input and no output)
- Consumer<T> – 1 object/type as input
- BiConsumer<T,U> – 2 object/type as input
- IntConsumer
- LongConsumer
- DoubleConsumer
- ObjIntConsumer<T> – 1object and 1 int as input
- ObjLongConsumer<T> – 1 object and 1 Long as input
- ObjDoubleConsumer<T> – 1 object and 1 double as input
- Java 8 Functions Interfaces (any input and any output)
- Function<T,R> – Type T as input and Type R as output (1 input and 1 output)
- BiFunction<T,U,R> – Type T and U as input and Type R as output (2 input 1 putout)
- IntFunction<R>
- LongFunction<R>
- DoubleFunction<R>
- IntToLongFunction
- IntToDoubleFunction
- IntToLongFunction
- LongToDoubleFunction
- LongToIntFunction
- DoubleToIntFunction
- DoubleToLongFunction
- ToIntFunction<T>
- ToIntBiFunction<T,U>
- ToLongFunction<T>
- ToLongBiFunction<T,U>
- ToDoubleFunction<T>
- ToDoubleBiFunction<T,U>
- Java 8 UnitaryOperator Interfaces (one input and same type as output)
- Java 8 BinaryOperator Interfaces (two input and same type as output)
- Java 8 Predicate Interfaces (always returns true or false)