What are the functional interfaces that were already present in Java 7? Can they be used as targets of lambda expressions?
- [Instructor] Hi. Welcome to the third section. This section is about functional interfaces. In particular, we're going to have a look at the variety of functional interfaces that can be found in the Java API. This section is divided into four videos. In the first video, we're going to consider the old functional interfaces that were already present into the Java 7 API.
Next, we're going to look at the new functional interfaces that were added to Java 8 and 9. In the third video, we're going to consider the special versions of those functional interfaces that deal with primitive types, and finally, in the fourth video, we're going to have a look at the number of methods that are offered by these interfaces to compose their objects into more powerful functions.
Let's start this first video about pre-existing functional interfaces. In this video we will describe a number of functional interfaces that were present in the Java API before Java 8, so before the notion of functional interface was officially added to the language, and these interfaces are the runnable and callable interfaces, the ActionListener interface, and the comparable and comparator interfaces.
Now, you may be wondering, how was it possible that Java had functional interfaces before Java 8, since the term "functional interface" itself was introduced into the language with version 8? Well, on one hand, the term "functional interface" was only introduced in version 8, on the other hand, this is just a shortcut for any interface with a single abstract method, therefore it's perfectly normal that you could find a number of interfaces with a single abstract method, even in Java 7.
And in this light, you can see five of the most common and useful functional interfaces that were already there in Java. So the first two have to do with multi-threading, they're called runnable and callable. Next, there is a functional interface which is very important in the AWT graphical libraries, this interface is called ActionListener. And finally, two interfaces that you probably already know, that we have already used in previous videos, they have to do with sorting objects, they're called comparable and comparator, and among these five interfaces, only comparator is a pure functional interface, meaning that it's intended to be implemented by stateless objects.
So, let's have a look at these interfaces one at a time, first one is runnable, this interface has a single abstract method, obviously, it's called "run," it has no arguments and no return values, and this interface is used to represent the body of a thread. Then for, usually what you do is you pass such an object, an object-implemented runnable to this constructor of the thread class, and a new thread will start, and this new thread will start its execution from the run method of the runnable object, so in other words, the runnable object, and in particular, its run method is the entry point of a new thread.
On a related note, the callable interface represents a thread returning a value of type V, so as you can see, it's a parametric interface, whose method returns an object of type V, and since this type of thread returns a value, you cannot use it directly with the thread class, but you can use it with something more sophisticated, called an ExecutorService, so you can pass a callable object to the submit method, and this method will return a future object, which represents the eventual return value from this thread, when the task will have finished.
Obviously the details of these mechanisms are outside the scope of this course, but at least you should know what these interfaces, where these interfaces come into play, and then you can find out further details. Next, there's ActionListener, this interface has a method called actionPerformed accepting an object of type ActionEvent, the meaning of this interface is to provide callback functions for graphical user interface events, in other words, consider the class Jbutton, which represents a clickable button in a graphical interface, you're supposed to call the method addActionListener, and pass an ActionListener to it, and then after you do that, every time the user clicks on this button, the actionPerformed method of your object will be called, and you can deal with this event in any way your program requires.
This is what a callback function is. And finally, we get to the last two interfaces, first there is comparable, its method is called compareTo, it takes another object of type T as argument, and it compares this object with the other object, and the comparison returns an integer. You can use such an interface to sort some collection, in particular to sort a list, so as you can see at the bottom of these lines, you can call this static method of the Collections class, called sort, you pass a list, and provided the elements of the list, implement the comparable interface, this method is going to sort the list.
Finally, there is comparator, it has a similar meaning to the comparable interface, but its method takes two objects of type T, and it compares these two objects and returns the comparison in the form of an integer. So, this one represents an external ordering criterion for a class, so it's an object, takes two other objects and compares them, and you can use it to, once again, to sort the list, this time you call these other versions of the sort method, you pass a list and a comparator, and your list is going to be sorted, and this time the elements of the list do not need to implement any special interface, because you're separately providing an ordering criterion via the comparator.
So, as I said, comparator is a pure functional interface, because it's intended to be implemented by stateless objects, and that's because comparing two objects normally requires no memory on the part of the comparator. On the other hand, the comparable is quite the opposite of a pure functional interface, so something is comparable if it can compare itself with another object of the same type, and in order for this comparison to make sense, this object and the other object, they have to have some state, they have to have instance fields, otherwise the comparison hardly makes any sense.
Okay, so that was all for this video, we learned that Java 7 came with a number of functional interfaces, having to do with comparancy, graphical user interfaces, and sorting, and one of these interfaces is actually pure, and that's comparator. If you're not familiar with the comparable and comparator interfaces, you may want to find out about their interactions with a number of standard collections from the collection framework.
Then we can now move to the next video, which is going to be about the new functional interfaces.
Note: To get the best results from this course, you should be familiar with basic Java programming concepts, including lists, maps, and sets, and have a suitable IDE, such as Eclipse, NetBeans, or IntelliJ.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- What is functional programming?
- What are functional interfaces?
- Writing lambda expressions
- Creating functional interfaces
- Composing functions
- Sequential data processing with streams
- Using parallel streams