Java 8's support for functional style programming
So, what is the foundation for functional style programming in Java 8? Well, it comes from a number of additions and modifications to the language. In this section, we will briefly introduce several concepts that Java 8 uses. These include:
- Lambda expressions
- Default methods
- Functional interfaces
- Method and constructor references
- Collections
Understanding these concepts will enable you to understand their purpose and why they are used.
Lambda expressions
Lambda expressions are essentially anonymous functions. They can be considered to be one of the most significant additions to Java 8. They can make the code easier to write and read.
We have already seen a lambda expression in the previous examples. In this section, we will provide additional detail about their form and use. There are three key aspects to lambda expressions:
- They are a block of code
- They may be passed parameters
- They may return a value
The following table illustrates several different forms a simple lambda expression can take:
Lambda expression |
Meaning |
---|---|
()->System.out.println() |
It takes no arguments and displays a single line |
x->System.out.println(x) |
It takes a single argument and displays it on a line |
x->2*x |
It takes a single argument and returns its double |
(x,y)->x+y |
It takes two arguments and returns their sum |
x -> { int y = 2*x; return y; } |
It takes a single argument and returns its double using multiple statements |
These examples are intended to provide some indication of what forms they may take on. A lambda expression may have zero, one, or more parameters and may return a value. They can be a concise single-line lambda expression or may consist of multiple lines. However, they need to be used in some context to be useful.
You can use a lambda expression in most places where a block of code needs to be executed. The advantage is that you do not need to formally declare and use a method to perform some task.
Lambda expressions are often converted to a functional interface automatically simplifying many tasks. Lambda expressions can access other variables outside of the expression. The ability to access these types of variables is an improvement over anonymous inner functions, which have problems in this regard. Lambda expressions will be discussed in more detail in Chapter 2, Putting the Function in Functional Programming.
Default methods
A default method is an interface method that possesses an implementation. Traditionally, interfaces can only contain abstract methods or static and final variables. This concept provides a way of defining a set of methods that a class can implement, and by doing so, it provides an enhanced form of polymorphic behavior.
Adding a default method to an interface is simple. The method is added using the default
keyword along with its implementation. In the following example, an interface called Computable
is declared. It has one abstract method and two default methods:
public interface Computable { public int compute(); public default int doubleNumber(int num) { return 2*num; } public default int negateNumber(int num) { return -1*num; } }
To use a default method, we create a class that implements its interface and executes the method against an instance of the new class. In the next sequence, the ComputeImpl
class is declared that implements the Computable
interface:
public class ComputeImpl implements Computable { @Override public int compute() { return 1; } }
Next, an instance of ComputeImpl
is declared, and the default method is executed:
ComputeImplcomputeImpl = new ComputeImpl(); System.out.println(computeImpl.doubleNumber(2));
The result will be a 4
. We did not have to provide an implementation of the doubleNumber
method in the ComputeImpl
class before we used it. However, we can override if desired.
In Java 8, we can add default and static methods to interfaces. This has a number of advantages, including the ability to add capability to previous interfaces without breaking the existing code. This has allowed interfaces declared prior to Java 8 to be augmented with a default method that supports functional-type operations.
For example, the forEach
method has been added as a default method to the java.lang
package's Iterable
interface. This method takes a lambda expression that matches the Consumer
interface's accept
method and executes it against each member of the underlying collection.
In the next code sequence, an array list is populated with three strings. The ArrayList
class implements the Iterable
interface enabling the use of the forEach
method:
ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Peach"); list.add("Banana"); list.forEach(f->System.out.println(f));
The addition of a default method will not break code that was developed before the method was added.
Functional interfaces
A functional interface is an interface that has one and only one abstract method. The Computable
interface declared in the previous section is a functional interface. It has one and only one abstract method: compute
. If a second abstract method was added, the interface would no longer be a functional interface.
Functional interfaces facilitate the use of lambda expressions. This is illustrated with the Iterable
interface's forEach
method. It expects a lambda expression that implements the Consumer
interface. This interface has a single abstract method, accept
, making it a functional interface.
This means that the forEach
method will accept any lambda expression that matches the accept
method's signature as defined here:
void accept(T t)
That is, it will use any lambda expression that is passed a single value and returns void. As seen with the ArrayList
class used in the previous section and duplicated next, the lambda expression matches the signature of the accept
method.
list.forEach(f->System.out.println(f));
This is possible because Java 8 uses a technique called type inference to determine if the lambda expression can be used.
Java 8 has introduced a number of functional interfaces. However, conceptually they have been present in earlier version of Java, but were not identified as functional interfaces. For example, the Runnable
interface, with its single abstract run
method, is a functional interface. It has been a part of Java since the very beginning, but until Java 8 was not labeled as a functional interface.
The advantage of functional interfaces is that Java is able to automatically use a lambda expression that matches the signature of the abstract method found in a functional interface. Consider the creation of a thread as illustrated in the following code sequence:
new Thread(()-> { for(inti=0; i<5; i++) { System.out.println("Thread!"); } }).start();
The argument of the Thread
class's constructor is a lambda expression that implements the Runnable
interface's run
method. This method takes zero argument and returns void. The lambda expression used matches this signature.
Method and constructor references
A method or constructor reference is a technique that allows a Java 8 programmer to use a method or constructor as if it was a lambda expression. In the following sequence, a stream is generated, its elements are doubled, and then displayed using a lambda expression:
Stream<Integer> stream = Stream.of(12, 52, 32, 74, 25); Stream .map(x -> x * 2) .forEach(x ->System.out.println(x));
The output follows:
24 104 64 148 50
We can duplicate this sequence using a method reference in place of the lambda expression as shown next. A method reference takes the form of a class name followed by a double colon and then the method name. The parameter is implied, and the code will produce the same output as the previous example.
Stream<Integer> stream = Stream.of(12, 52, 32, 74, 25); Stream .map(x -> x * 2) .forEach(System.out::println);
In the following example, two method references are used where the first one invokes the sin
method against each element of the list:
stream .map(Math::sin) .forEach(System.out::println);
The output follows:
-0.5365729180004349 0.9866275920404853 0.5514266812416906 -0.9851462604682474 -0.13235175009777303
We can also use constructors in a similar manner. Method and constructor references provide a convenient and easy way of using methods and constructors where lambda expressions are used.
Collections
The Collection
interface has been enhanced in Java with the addition of methods that return a Stream
object based on the collection. The stream
method returns a stream executed in sequential order while the parallelStream
method returns a stream that is executed concurrently. The following example illustrates the use of the stream
method as applied against the list
object. The List
interface extends the Collection
interface:
String names[] = {"Sally", "George", "Paul"}; List<String> list = Arrays.asList(names); Stream<String> stream = list.stream(); stream.forEach(name ->System.out.println(name + " - " + name.length()));
This sequence output follows:
Sally - 5 George - 6 Paul - 4
In addition, since the Collection
interface inherits the Iterable
interface's forEach
method from the iterator. We can use this with the previous List
object:
list.forEach(name ->System.out.println(name + " - " + name.length()));
There are other enhancements to collections in Java 8, which we will present as they are encountered.