I usually use scripting languages.
I had a rare opportunity to touch Java, so About Java lambda expressions that I knew only a little Although it is the time of Java 11 release, I will summarize it for confirmation.
Let's say you're in a great game development team. I will be in charge of the part related to the user's name. In the future, the title of the main character will change for each character, so I was entrusted with the development of the API around that.
Defines the interface responsible for formatting the name.
NameFormatter.java
interface NameFormatter {
public abstract String format(String familyName, String firstName);
}
The implementation class and the main execution part look like this.
Chanformatter.java
class ChanFormatter implements NameFormatter {
@Override
public String format(String familyName, String firstName){
return firstName + "Chan";
}
}
Main.java
class Main {
public static void main(String[] args){
String familyName = "Date";
String firstName = "Masamune";
NameFormatter nf = new ChanFormatter();
String formatted = nf.format(familyName, firstName);
System.out.println("Hello!" + formatted + "。");
System.out.println("I'm starting a great game!");
}
}
Click here for the execution result
Hello! Masamune-chan.
I'm starting a great game!
It's familiar.
On the contrary, let's prepare another formatter for the arrogant character.
DonoFormatter.java
class DonoFormatter implements NameFormatter {
@Override
public String format(String familyName, String firstName){
return familyName + "Tono";
}
}
You can easily change the name by simply changing the new class in the main execution section.
Main.java(diff)
String familyName = "Date";
String firstName = "Masamune";
- NameFormatter nf = new ChanFormatter();
+ NameFormatter nf = new DonoFormatter();
String formatted = nf.format(familyName, firstName);
System.out.println("Hello!" + formatted + "。");
Hello! Date.
I'm starting a great game!
Now, let's say you get fired from a great game development team. This is because it is not possible to spend personnel costs on the maintenance of such a small class group. The development team decided to implement the formatter using an anonymous class.
Instead of creating individual files for a class of this size Anonymous class is a function that can be defined on the spot and thrown away.
With the anonymous class, you only need the following two files:
Nameformatter.java
//Repost
//Same as above
interface NameFormatter {
public abstract String format(String familyName, String firstName);
}
Main.java
class Main {
public static void main(String[] args){
NameFormatter chan = new NameFormatter(){
@Override
public String format(String familyName, String firstName){
return firstName + "Chan";
}
};
NameFormatter dono = new NameFormatter(){
@Override
public String format(String familyName, String firstName){
return familyName + "Tono";
}
};
String familyName = "Date";
String firstName = "Masamune";
String formatted = chan.format(familyName, firstName);
System.out.println("Hello!" + formatted + "。");
System.out.println("I'm starting a great game!");
}
}
It is a form in which the format method is linked to the variable.
Well, this is the lambda expression of the main subject.
In the situation like this time
The only method that the NameFormatter
class should implement is format
.
Therefore, you can only override the format
method.
I feel that it is redundant to copy and paste each time with public String format (String familyName, String firstName)
. I want you to do my best to the compiler.
The lambda expression is a notation that can fulfill such a request. It can be written as follows.
Main.java
class Main {
public static void main(String[] args){
NameFormatter chan = (String familyName, String firstName) -> {
return firstName + "Chan";
};
NameFormatter dono = (String familyName, String firstName) -> {
return familyName + "Tono";
};
String familyName = "Date";
String firstName = "Masamune";
String formatted = chan.format(familyName, firstName);
System.out.println("Hello!" + formatted + "。");
System.out.println("I'm starting a great game!");
}
}
I was able to write clearly. that's all.
That's it for lambda expressions. The lambda expression itself is just an anonymous class abbreviation, not a big topic.
The main enclosure is not a lambda type, but the following two points.
--Functional interface (package java.util.function
)
This time, I used the original interface called NameFormatter
,
Many interfaces are provided that are intended to use lambda expressions.
One of them is java.util.function.Function <T, R>
.
We've also added a number of common functional method methods such as map
and filter
as the Java Stream API.
These methods are used by passing the interface of java.util.function
as an argument.
The lambda expression is not scary.
I want to be able to use the Stream API smoothly
Understanding Java 8 Lambda Expressions-Qiita
Recommended Posts