A Java user over a dozen years ago tried to study the functions of Java8 (Lambda expression).

Introduction

Recently, I was blessed with the opportunity to use Java because of my work. I updated my ancient Java (1.3) knowledge.

What is a lambda expression?

This is a function to easily implement a functional interface.

This alone is refreshing, so I would like to explain it with an example.

Compare lambda expressions with code

Code that doesn't use lambda expressions

public static void main(String[] args) {

  class Sample implements Runnable {
    public void run() {
      System.out.println("I Love Pengin!");
    }
  }

  Runnable runner = new Sample();
  times( 10, runner );
}

public static void times( int count, Runnable runner ) {
  for( int i = 0; i < count; i++ ) {
    runner.run();
  }
}

Code using lambda expressions

public static void main(String[] args) {

  funcCall( 10, () -> System.out.println("I Love Pengin!") );
}
public static void times( int count, Runnable runner ) {
  for( int i = 0; i < count; i++ ) {
    runner.run();
  }
}

What is good to use?

The example is the processing to the times function that repeats the specified processing multiple times. If you compare them, you can see that the lambda expression is quite complete.

The method of substituting a part of the processing with another object is called delegation. By using a lambda expression, I think the advantage is that this delegation is much easier to write.

How to write a lambda expression

Format

Let's look at the format when using a lambda expression

() -> System.out.println("I Love Pengin!")
  1. The first () represents the method argument. We don't use arguments this time, but you can use them.
  2. -> describes the actual processing content. If the processing content spans multiple processes, write -> {Processing} here.

Lambda expression with arguments

When using

By specifying the argument of the lambda expression, the argument can be used during processing.

(i) -> System.out.println("I Love Pengin!" + i)

When making

It is necessary to use the function interface properly according to the type of argument and return value to be used. If there is one argument and no return value, it will be as follows.

public static void times( int count, Consumer<Integer> runner ) {
  for( int i = 0; i < count; i++ ) {
    runner.accept(new Integer(i));
  }
}

The interface to use properly is as follows.

type Function interface Method
0 arguments that do not return a value Runnable run
1 argument that returns no value Consumer accept
2 arguments that do not return a value BiConsumer accept
0 arguments that return a value Supplier get
1 argument that returns a value Function apply
2 arguments that return a value BiFunction apply
1 argument that returns a boolean value Predicate test
Two arguments that return a boolean value BiPredicate test
1 argument that returns the operation result UnaryOperator test
2 arguments that return the operation result BinaryOperator test

Recommended Posts

A Java user over a dozen years ago tried to study the functions of Java8 (Lambda expression).
A Java user over a dozen years ago tried to study the functions of Java8 (Generics).
Pass the condition to be used in the Java8 lambda expression filter () as a parameter
Assign a Java8 lambda expression to a variable and reuse it
I tried to summarize the basics of kotlin and java
The idea of C # (lambda expression, for statement) to chew
I tried to make a client of RESAS-API in Java
Replace with a value according to the match with a Java regular expression
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
I tried to summarize the methods of Java String and StringBuilder
[Java] I tried to make a maze by the digging method ♪
How to check for the contents of a java fixed-length string
A quick look back at Java over the last five years
What is a lambda expression (Java)
The origin of Java lambda expressions
The story of forgetting to close a file in Java and failing
How to find out the Java version of a compiled class file
[Java] How to get to the front of a specific string using the String class
I tried to visualize the access of Lambda → Athena with AWS X-Ray
How to get the absolute path of a directory running in Java
I want to ForEach an array with a Lambda expression in Java
Now let's recap the Java lambda expression
I tried to summarize Java lambda expressions
What I tried when I wanted to get all the fields of a bean
How to get the ID of a user authenticated with Firebase in Swift
Send a notification to slack with the free version of sentry (using lambda)
Validate the identity token of a user authenticated with AWS Cognito in Java
[Small story] I tried to make the java ArrayList a little more convenient
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
I tried to make a product price comparison tool of Amazon around the world with Java, Amazon Product Advertising API, Currency API (2017/01/29)
Transform from a normal class to a lambda expression
Make a margin to the left of the TextField
[Java] Summary of how to abbreviate lambda expressions
Measure the size of a folder in Java
I tried to develop a DUO3.0 study website.
Set the time of LocalDateTime to a specific time
How to use Java API with lambda expression
Java8 to start now ~ forEach and lambda expression ~
I tried to break a block with java (1)
Java Welcome to the Swamp of 2D Arrays
[Java] Cut out a part of the character string with Matcher and regular expression