Lambda expressions in Java

Before moving on, we need to learn about a feature in Java called Lambda. Many of you may already know about it. However, since the feature was only introduced in version 8, it is better to get familiar with it if you aren't already. It lets you pass a block of code, called a lambda expression, as an argument to another function. To talk about lambda, we must first see what a functional interface is.

Functional interface

A functional interface is an interface that has only one unimplemented method, that is to say, a class that implements it needs to implement exactly one method. The functional interface may have more than one method declared or inherited, but as long as we can implement it by implementing exactly one method, it is a functional interface. The following example shows one such interface:

@FunctionalInterface
public interface SampleFunctionalInterface {
  int modify(int x);
}

Note that we also marked it as a functional interface with an annotation, but it is not necessary. Marking it ensures that Java will show a compile-time error if the interface does not have exactly one method that needs to be implemented. The following example shows another valid functional interface:

public interface AnotherFunctionalInterface{
  public void doSomething(int x);
  public String toString();
}

There are two methods in it. However, since the toString() method is already implemented in the object class, you need to implement only one method.

Similarly, an interface with more than one method can be a functional interface if all but one has a default implementation. For example, look at the following interface.

@FunctionalInterface
public interface FunctionalInterfaceWithDefaultMethod {
    int modify(int x);
    default int modifyTwice(int x){return modify(modify(x));}
}

Even though this interface has two methods, only one needs to be implemented by any implementation. This makes it a functional interface.

Implementing a functional interface with lambda

So, what happens if we have a functional interface? We can provide an inline implementation of it using a cool syntax called lambda, as follows:

SampleFunctionalInterface sfi = (x)->x+1;
int y = sfi.modify(1);

Take note of the parentheses and the arrow sign. The parentheses contain all the parameters. The types of parameters are not specified because they are already specified in the interface method. There can be zero or more parameters.

There are two kinds of lambda syntax–one that has an expression as the body and one that has one or more steps as the body. These lambdas look a bit different from each other. A lambda that is implemented as a one liner looks like the one we just saw. This is called an expression syntax. The expression syntax can be used if the lambda expression is a one liner. For multi-line code, we use the block syntax as shown below:

Thread t = new Thread(()->{for(int i=0;i<500;i++) System.out.println(i);});

One can use block syntax for functions that return a value as well, especially when using multiple lines of code. In that case, one just needs to use a return statement to return a value.

Note

Since in a functional program all variables must not ever be reassigned, we should declare them final to avoid accidentally modifying them. However, since typing final for every variable clutters the code a bit, we avoid doing so. In a purely functional language, the variables are immutable by default. Even in a semifunctional language, such as Scala, it is so if it generally encourages the functional style. However, since Java mostly prefers an imperative style, the final keyword is necessary, causing a little clutter.

Now that we know about lambda, we can start learning about functional data structures.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset