Outils pour utilisateurs

Outils du site


lang:java:lambda

Exemple avec les foncteurs :

public class Test {
  public Boolean f() {
    System.out.println("Fonction");
    return true;
  }
 
  public void m() {
    System.out.println("Méthode void");
  }
 
  public static void s() {
    System.out.println("Méthode statique");
  }
}
 
import java.util.function.Consumer;
import java.util.function.Function;
 
@FunctionalInterface
interface VoidFunction{  
  void execute();  
}  
 
@FunctionalInterface
interface BooleanFunction{  
  Boolean execute(Test t);  
}  
 
public class Main {
  public static void main(String[] args) {
    Function<Test, Boolean> f = Test::f;
    Consumer<Test> c = Test::m;
    VoidFunction ss = Test::s;
    BooleanFunction b = Test::f;
 
    Test t = new Test();
    f.apply(t);
    c.accept(t);
    ss.execute();
    b.execute(t);
  }
}
lang/java/lambda.txt · Dernière modification : 2020/03/05 22:47 de root