fork download
  1. // java 8 lambdas
  2. class Main{
  3. // Interfaces that are used for lambdas may only contain a single method.
  4.  
  5. interface A{
  6. void a(); // No argument nor return
  7. }
  8. static A a = ()->{System.out.println("A A");};
  9.  
  10. interface B{
  11. void b(String s); // String argument, no return
  12. }
  13. static B b = s->{System.out.println(s);};
  14.  
  15. interface C{
  16. String c(); // No argument, String return
  17. }
  18. static C c = ()->"C C"; // short for ()->{return "C C";};
  19.  
  20. interface D{
  21. String d(String s); // String for both the argument and return
  22. }
  23. static D d = s->s+" D"; // short for s->{return s+" D";};
  24.  
  25. // Runnable takes no argument and has no return
  26. static java.lang.Runnable e = ()->{System.out.println("E E");};
  27. // Consumer takes arguments, but has no return
  28. static java.util.function.Consumer<String> f = s->{System.out.println(s);};
  29. // Supplier takes no arguments, but has a return
  30. static java.util.function.Supplier<String> g = ()->"G G"; // short for ()->{return "G G";};
  31. // Function takes arguments, and has a return
  32. static java.util.function.Function<String, String> h = s->s+" H"; // short for s->{return s+" H";};
  33.  
  34. interface X{
  35. String x(String a, String b); // It is also possible to have multiple arguments
  36. }
  37. static X x = (a,b)->a+" "+b; // short for (a,b)->{return a+" "+b;};
  38.  
  39. // It is also possible to chain lambda's. This is called currying, and is useful for codegolfing,
  40. // because a->b->{...} is 1 byte shorter than (a,b)->{...}
  41. static java.util.function.Function<String, java.util.function.Function<String, String>> y = a->b->a+" "+b; // short for a->b->{return a+" "+b;};
  42. // NOTE:
  43. // - With no arguments currying-lambdas and interface-lambdas are of equal length: ()->{...} vs ()->{...}
  44. // - With 1 argument currying-lambdas and interface-lambdas are of equal length: a->{...} vs a->{...}
  45. // - With 2 arguments currying-lambdas are 1 byte shorter than interface-lambdas: a->b->{...} vs (a,b)->{...}
  46. // - With 3 arguments currying-lambdas and interface-lambdas are of equal length: a->b->c->{...} vs (a,b,c)->{...}
  47. // - With 4 or more arguments currying-lambdas are longer than interface-lambdas: a->b->c->d->{...} vs (a,b,c,d)->{...}
  48.  
  49. // Using a Void null argument is also useful for code-golfing, because v->{...} is 1 byte shorter than ()->{...}
  50. interface Z{
  51. String z(Void v);
  52. }
  53. static Z z = v->"Z Z"; // short for v->{return "Z Z";};
  54.  
  55. public static void main(String[] args){
  56. a.a();
  57. b.b("B B");
  58. System.out.println(c.c());
  59. System.out.println(d.d("D"));
  60. e.run(); // Runnable has the run() method to execute the lambda
  61. f.accept("F F"); // Consumer has the accept(...) method to execute the lambda
  62. System.out.println(g.get()); // Supplier has the get() method to execute the lambda
  63. System.out.println(h.apply("H")); // Function has the apply(...) method to execute the lambda
  64. System.out.println(x.x("X","X"));
  65. System.out.println(y.apply("Y").apply("Y"));
  66. System.out.println(z.z(null));
  67. }
  68. }
Success #stdin #stdout 0.16s 37116KB
stdin
Standard input is empty
stdout
A A
B B
C C
D D
E E
F F
G G
H H
X X
Y Y
Z Z