Lambda Expressions

  • 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

  1. Functional Interfaces and its types
    1. Java 8 Predicate Interfaces (always returns true or false)
      1. Predicate<T> – 1 object/type as input
      2. BiPredicate<T,U> – 2 object/type as input
      3. IntPredicate – 1 Integer as input
      4. LongPredicate – 1 Long as input
      5. DoublePredicate – 1 Double as Input
    2. Java 8 Supplier Interfaces (no input and only output)
      1. Supplier<T>- 1 object/type as output
      2. BooleanSupplier – 1 boolean as output
      3. IntSupplier – 1 Integer as output
      4. LongSupplier – 1 Long as output
      5. DoubleSupplier – 1 Double as output
    3. Java 8 Consumer Interfaces (only input and no output)
      1. Consumer<T> – 1 object/type as input
      2. BiConsumer<T,U> – 2 object/type as input
      3. IntConsumer
      4. LongConsumer
      5. DoubleConsumer
      6. ObjIntConsumer<T> – 1object and 1 int as input
      7. ObjLongConsumer<T> – 1 object and 1 Long as input
      8. ObjDoubleConsumer<T> – 1 object and 1 double as input
    4. Java 8 Functions Interfaces (any input and any output)
      1. Function<T,R> – Type T as input and Type R as output (1 input and 1 output)
      2. BiFunction<T,U,R> – Type T and U as input and Type R as output (2 input 1 putout)
      3. IntFunction<R>
      4. LongFunction<R>
      5. DoubleFunction<R>
      6. IntToLongFunction
      7. IntToDoubleFunction
      8. IntToLongFunction
      9. LongToDoubleFunction
      10. LongToIntFunction
      11. DoubleToIntFunction
      12. DoubleToLongFunction
      13. ToIntFunction<T>
      14. ToIntBiFunction<T,U>
      15. ToLongFunction<T>
      16. ToLongBiFunction<T,U>
      17. ToDoubleFunction<T>
      18. ToDoubleBiFunction<T,U>
    5. Java 8 UnitaryOperator Interfaces (one input and same type as output)
      1. UnaryOperator<T>
      2. IntUnaryOperator
      3. LongUnaryOperator
      4. DoubleUnaryOperator
    6. Java 8 BinaryOperator Interfaces (two input and same type as output)
      1. IntBinaryOperator
      2. LongBinaryOperator
      3. DoubleBinaryOperator

Leave a Comment