First-class functions in Java 8

preview_player
Показать описание


0:15 First-class functions in Java
In this first video, you'll see how to write more flexible code by regarding methods as first class-values, and using lambda expressions to concisely represent behaviors. Let's start by looking at the idea of functions as first-class values.
2:00 Methods as first class-citizens
Programs embracing the idea of methods as values are said to be functional-style programs—which essentially just means "programs that pass functions around as first-class values." The designers of Java 8 decided to allow methods to be values.
The first new Java 8 feature we introduce is called method references.
One of the most significant additions is lambdas, or anonymous functions.
3:40 Passing code example
Suppose you have a farm inventory application with a class Apple declaring a method getColor, and an inventory that's a list of all Apple objects.
It's reasonable to think you that might want to select all the green apples and return them in a list. You'd typically call the code to select just the green apples a filter. In this case, you'd write your filter as a method called something like filterGreenApples: Let's look at an example of what this method might look like.
Java 8 makes it possible to pass the code of the whole condition as an argument, thus avoiding code duplication of the filter method.
Let's look at how you can write this, and step through some of the important features of this code.To use this you simply call filterApples with parameter inventory and a method reference to either isGreenApple or isHeavyApple. The key idea to take away for now is that you can pass a method around in Java 8!
6:22 Introducing Java 8 Lambdas
While passing code is a way to give new behaviors as an argument to a method, it's currently quite verbose in Java. Lambdas, also known as anonymous functions, help eliminate the verbosity of declaring multiple concrete classes for an interface that's needed only once.
In our previous apple-sorting example, we showed you that passing methods is useful. We also reminded you that it's really annoying to write separate definitions for the methods isHeavyApple, isGreenApple, and any other required condition, especially when they're likely to be used only once or twice. Fortunately, Java 8 has solved this too.
Enter the lambda expression. The new lambda notation lets you represent your method differently. Lambda expressions follow a straightforward syntax, shown here. Now let's look at a few specific examples based on our earlier farm inventory code. As you can see, using this new approach, you don't need to write a method definition that's used only once; the code is crisper and clearer because you don't need to search to find the code you're passing.
A lambda expression can be understood as a kind of anonymous function that can be passed around: it doesn't have a name, but it has a list of parameters, a body, a return type, and also possibly a list of exceptions that can be thrown. Lambdas fix the verbosity problem because they let you pass code in a concise way. Thus, you can cope with changing requirements by using a behavior, represented by a lambda, as a parameter to a method.
Once you get used to them, you'll find lambdas to be pretty powerful.
Рекомендации по теме
Комментарии
Автор

Great. I would recommend that every chapter from Manning books should have a video lesson on witch author explain and show some live coding.Or  a video tutorials section.

luciangx
Автор

I liked the expressions on the apple's face when the scale tilted to show it was overweight :-)

amitp
Автор

Looking forward to more videos like this

ScottDamery
Автор

Enlightening tutorial on Lambada Expression !! Keep Posting

damajikalunge
Автор

Awesome. Thanks for greate explanation.

IliaKaygorodov
Автор

Masked Super Hero Lambda is the cutest thing!

ThuyNguyen-buge
Автор

good video but it is still verbose lambda in java.

LambdaPool
Автор

Java 8 hỗ trợ lamda function, firstclass function. Sao mình cảm giác nó đang tiệm cận về Scala.

Scala ra đời đã hơn 10 năm, nhưng thực sự đã thay thế được Java chưa? Hay là ngược lại? 

P.S.: dạo này có vẻ nghe về  JS nhiều
hơn 

ThuanLecse
Автор

The only time I am a nerdy geek is here.

QASIMARA
Автор

Excellent! Just don't forget to not excluding female programmers, please. Even in pictures.

linakrantz