Lambda Expressions in Java - What's New in Java 8 Part 1
Get complete courses at http://courses.caveofprogramming.com
An introduction to lambda expressions, the most important new language feature in Java 8. Lambda expressions allow you to pass blocks of code to methods; something that could previously only be achieved by implementing an interface.
================================
Hello,
This is John from caveofprogramming.com and in this tutorial we're going to look at lambda expressions in Java 8, so I've created a new project here and I've already got JDK8 installed, which you can see here, and I'm using it for this particular project. You might need to download a new eclipse to do this.
So, lambda expressions are just a way of passing a block of code to a method. That's all they are. And I'm going to first go through how we would do this, very briefly, in previous versions of Java, and to understand this you need to have an understanding of interfaces and of implementing anonymous classes as well.
So, typical situations when you might want to do this would be when you want to pass code to a thread, to execute it in its own thread, and where you want to pass code to a button, let's say in Java Swing, so that code can be executed when the button is clicked. If you want a some revision of any of this, I've got a massive free Java course that includes interfaces and anonymous classes and also a free course on multi-threading and also on Swing, and you can find those on caveofprogramming.com.
So let's start by creating a class here. I'm going to create a class, let's call it "class Runner", just to give it a name, and I'm going to give this method which I'll call "public void run". Now let's imagine that for some reason we want to pass a block of code to run, so we don't want to pass in just strings, we want to actually pass a block of code.
How would you do that in previous versions of Java? Well, the answer is first you need to define an interface, let's do that, let's say "interface" and I'll call this "Executable" and we need to give this interface a method, and this method would be a place where we put the code we want to run. Let's create a method called "void execute", just like that.
Now we can say that the runner accepts an object that implements the executable interface. I'll just call it "e". So its going to get something that implements this interface and its going to know that that object, whatever it is, has an execute method, so we can call that method, let's put sysout in here and say "executing code block..." and let's just call "e.execute()".
Now to actually use this, so firstly we need an instance of this runner class, let's say "Runner runner = new Runner" and then we can call the run method so let's say "runner.run".
Now in previous versions of Java, to actually pass it some block of code we've got to somehow have an object that implements this interface, and we can do that using an anonymous class syntax. In previous versions of Java, we can say here "new executable", in this case. Open and close curly brackets and let's implement the method that this interface specifies. So I'll get rid of these lines that we don't actually need and we can put some code in that to execute. Let's say sysout "hello there", so this is all Java 6 or 7 syntax
and if we now run this program, it says "hello there".
So the whole point of this is just to pass in this code to this method and then this method can and do what it likes with it, and in this case it's just outputting something and then it's actually executing the code that we passed it in this method. So this is a lot of text purely just to specify this, so let's have a look at how we can do that with Lambda Expressions in Java 8. I'm going to just put a bit of separator text here; let's just put in like some equal signs.
By the way, "Lambda Expressions" apparently comes from mathematics where the Greek letter lambda has been historically associated with some kind of analogous situation, passing a function to a function, something like that.
So to use the Lambda Expression here we can say "runner.run". So the same thing again; we're calling this method and now again the challenge is to pass this method a block of code to execute, and we can pass this in a lot more elegantly with the Lambda Expression. We need two empty round brackets and an arrow. So it's just an hyphen and a right angle bracket, and then the expression we want to run. So this bit here is the lambda expression. I'm going to just paste this up here because I'm going to make this code available on caveofprogramming.com so you can refer back to it.
If you run this now, see, it does the same thing as this unwieldy syntax that we had in previous versions of Java. So it's really important to keep in mind that all this is a block of code that this method is going to execute, and blocks of codes, of course, might have returned values and might also accept parameters ...
-
Category
No comments found