Now let's recap the Java lambda expression

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.

1. Before the introduction of lambda expression

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!

1.1. Anonymous class

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.

2. Lambda expression

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.

3. That's it?

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.

4. Conclusion

The lambda expression is not scary.

5. Impressions

I want to be able to use the Stream API smoothly

6. Reference

Understanding Java 8 Lambda Expressions-Qiita

Recommended Posts

Now let's recap the Java lambda expression
[Java] Lambda expression
Java lambda expression
java neutral lambda expression 1
Java lambda expression variations
Java 8 lambda expression Feature
java lambda expression memo
Java lambda expression [memo]
Studying Java 8 (lambda expression)
Review java8 ~ Lambda expression ~
Java lambda expression again
Java8 to start now ~ forEach and lambda expression ~
[Java] Functional interface / lambda expression
Java8 stream, lambda expression summary
Java basic learning content 9 (lambda expression)
What is a lambda expression (Java)
The origin of Java lambda expressions
AWS Lambda with Java starting now Part 1
Let's study Java
[LeJOS] Let's control the EV3 motor with Java
How to use Java API with lambda expression
Hello Java Lambda
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -
Pass the condition to be used in the Java8 lambda expression filter () as a parameter
[Java] Let's declare variables used in the loop in the loop [Variables in the block]
[For beginners] Quickly understand the basics of Java 8 Lambda
[Java] Sort the list using streams and lambda expressions
Use Java lambda expressions outside of the Stream API
Quick learning Java "Introduction?" Part 2 Let's write the process
[LeJOS] Let's remotely control the EV3 motor with Java
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Java8 Lambda Expression & Stream Design Pattern Rethinking --Null Object Pattern -
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
Let's touch on Java
java learning (conditional expression)
Quarkus saves Java Lambda! ??
Understand Java 8 lambda expressions
Let's understand the function!
Review Java annotations now
About Java lambda expressions
A Java user over a dozen years ago tried to study the functions of Java8 (Lambda expression).
Introduction to lambda expression
Explain Java 8 lambda expressions
Let's scrape with Java! !!
Java table expression injection
java regular expression summary
Java Lambda Command Pattern
Assign a Java8 lambda expression to a variable and reuse it
Let's make a calculator application in Java ~ Display the application window
I want to use the Java 8 DateTime API slowly (now)
The idea of C # (lambda expression, for statement) to chew
Java8 Lambda Expression & Stream Design Pattern Rethinking --Chain of Responsibility Pattern -
Tips around the sample to release Java Lambda on CodePipeline