Lambda Expression: Definition

Before reading this tutorial try to Understand Java 8 Functional interfaces in 5 min

Lambdas expression is considered the biggest novelty in Java 8, it is a form of method, more compact than a standard method, for which the name is implicit and the parameters can be omitted, as well as their type or a value of return.

Lambda Expression is a simplified way for implementing an interface that has only one abstract method.


Lambda Expressions are anonymous functions

Syntaxe:

() -> System.out.println("Hello Lambda")
x -> x + 10
(int x, int y) -> { return x + y; }
(String x, String y) -> x.length() – y.length()
(String x) -> {	listA.add(x);	listB.remove(x);	
return listB.size();	         }

Lambda Expression: Target Type

In Java the type of a lambda expression is known as a functional interface.

Since a functional interface can only have one abstract method, the types of the parameters of the Lambda expression must match the parameters of that method, and the type of the body of lambda must match the return type of the method.

Lambda Expression : Implementations of Functional Interfaces

Function:
A “Function” interface takes one argument and returns a result whose functional method is “apply (Object)”.

Supplier:

The “Supplier” interface produces a result of a given type. Unlike the “Function” interface, “Supplier” does not accept arguments. The abstract method of this interface is “get ()”.

Consumer:
The “Consumer” interface represents an operation that accepts a single argument and returns no results. The abstract method of this interface is “accept (Object)”.

Predicate:

L’interface  “Predicate” représente une opération qui accepte un seul argument et renvoie un  résultat de type “Boolean”. La méthode abstraite de cette interface est “test(Object)”.

Commentaires

  1. Pingback: Comparator example Java 8 Lambda expression » JavaTuto

Laisser un commentaire