The focus of this series is on practical usage of lambdas, not in principal on the fundamental concepts of lambdas. You don't need to have any theoretical knowledge of lambda calculus or a strong background in functional programming. And don't break your mind on what closures are, just think of them as anonymous functions or code blocks that can be passed around. But despite you don't need to know about all this, I don't want to discourage you from researching these topics.
Note: If you want to try out some examples there's a little trick to get the lambda prototype working, see here.
Lambda expressions
To get started, let's have a look at a very simple example of an anonymous function or "lambda expression". The following is a lambda expression that takes an integer
x
and returns 2*x
:- #(int x)(2*x)
x
and y
and returns their sum:- #(int x, int y)(x+y)
#(int x)(2*x).(7)
, but this will probably be a bit uncommon. Function types
Instead invocation will mostly happen on variables of a function type. So we first bind a function to variable and do the invocation on that. The syntax for function types is very similar to the syntax of function literals:
- #int(int) doubler = #(int x)(2*x);
doubler
of type #int(int)
, i.e. of type function that takes an int (in parentheses) and returns an int (after the #). Now, to invoke doubler
we can write- int n = doubler.(3);
- assert n == 6;
- #int(int, int) sum = #(int x, int y)(x+y);
- int x = sum.(3, 7);
- assert x == 10;
So far the body of the lambda expressions was just a single expression. In this case it can be included in parentheses and the
return
keyword can be omitted. In the more complex case the body can be a block with curly braces and must explicitly return a value (if it's not void
):- #int(int, int) max = #(int x, int y) {
- if (x >= y) return x;
- else return y;
- };
- int z = max.(3,4);
- assert z == 4;
Of course, functions can also be passed as arguments to methods and other functions. This will be the most common usage. A function, that takes an integer
n
and function f
, that executes f
n
times could look like this:- public static void times(int n, #void(int) f) {
- for (int i=0;i<n;i++) {
- f.(i);
- }
- }
- times(5, #(int index)(System.out.println(index*index)));
x
and returns a function that takes another integer y
and returns x*y
.- public static #int(int) multiplier(final int x) {
- return #(int y)(x*y);
- }
- #int(int) mult5 = multiplier(5);
- assert 20 == mult5.(4);
x
is a free variable and its definition is copied over into the body of the lambda expression at runtime. For this to work x
must be declared effectively-final or shared (see straw-man proposal for details).That's it
That's basically it for the fundamentals of lambda expression in Java. But you will probably have noticed by now, that it will have a huge impact on Java, both the language and the libraries.
Side note: Some people don't like the syntax of lambdas as above. I don't want to start the discussion here again, just two points. First, the syntax can actually be awkward in some cases, but most of the time it's just passing around functions as literals or variables, which doesn't look awkward. And second, because Java is a statically typed language and has features like checked exceptions and others, closures won't look like in dynamically typed languages or languages with strong type inferencing or languages without checked exceptions.
Function conversion
This last section is about function conversion, which isn't something that is essential to lambda expressions, but will also have a huge impact. Many Java libraries use so called SAM types (single abstract method) - interfaces with only a single method and abstract classes with only one abstract method. Function conversion means that a function of appropriate type can be converted into an anonymous instance of a SAM type as needed. For example, the
Collections.sort
method takes a List
and a Comparator
, which has a single abstract method int compare(T x, T y)
. Up to now this would look like this:- // This would be just '= [4,2,1,3]'
- // with collection literals
- List<Integer> list =
- Arrays.asList(new Integer[]{4,2,1,3});
- Collections.sort(list, new Comparator<Integer>() {
- public int compare(Integer x, Integer y) {
- return -x.compareTo(y);
- }
- });
- Collections.sort(list,
- #(Integer x, Integer y)(-x.compareTo(y)));
Final example
For a final example we implement the Fibonacci function and call it several times in parallel threads.
- class ParallelFib {
- final static #void(int,int) fib =
- #(int c, int n) {
- int result = fib(n);
- System.out.println(c + ") " +
- "fib(" + n + ") = " + result);
- };
- public static int fib(int n) {
- if (n == 0 || n == 1) return 1;
- else return fib(n-1) + fib(n-2);
- }
- public static void main(String[] args) {
- for (int i=0;i<10;i++) {
- final int i2 = i;
- new Thread(#()(fib.(i2, 32))).start();
- }
- }
- }
fib
first occurs as a class variable of function type. This lambda expression takes a counter c
and input n
, calls the method fib (I don't know, if recursive lambda calls are possible at the moment) and then prints the counter and the result. The main method creates 10 threads each taking the fib lambda expression, which is converted implicitly into a Runnable
. The output is something like this:
- 1) fib(32) = 3524578
- 3) fib(32) = 3524578
- 2) fib(32) = 3524578
- 0) fib(32) = 3524578
- 7) fib(32) = 3524578
- 4) fib(32) = 3524578
- 9) fib(32) = 3524578
- 6) fib(32) = 3524578
- 8) fib(32) = 3524578
- 5) fib(32) = 3524578