[JAVA] Transform from a normal class to a lambda expression

Introduction

Hello. It's Kecho. ** Do you guys use lambda expressions? ** ** I will use it for the first time from now on. I'm an amateur.

After that, it will be transformed

100% class

First, the class that implements the Ruunable interface is as follows.

class lambda implements Runnable {
	@Override
	public void run() {
		System.out.print("OK");
	}
}

Implement the run method according to the interface and finish.

Class degree 60%, Lambda degree 40%

Runnable runner = new Runnable() {
	@Override
	public void run() {
		System.out.print("OK-ish");
	}
}; //OK-ish

Here the class is anonymized and defined in the same place as the instantiation. If you omit the last ";", a compile error will occur. Please let me know.

Class degree 40%, Lambda degree 60%

Redundant parts will be deleted by type inference.

Runnable runner = {
	@Override
	public void run() {
		System.out.print("OK-ish");
	}
}; //OK-ish

new runnable()Can guess the type from the left side.

Class degree 20%, Lambda degree 80%

Runnable runner = {
		{
		System.out.print("OK-ish");
}; //OK-ish

public void run() Since the runnable interface only has a run method, you don't have to specify it.

Lambda degree 100%

Runnable runner = () -> System.out.print("OK-ish");// OK-ish

Add the argument ``` ()->` `` and omit the {} to complete the lambda expression.

Summary

Is it possible to write like JS? ?? ??

Recommended Posts

Transform from a normal class to a lambda expression
Introduction to lambda expression
Ruby Regular Expression Extracts from a specific string to a string
What is a lambda expression?
Assign a Java8 lambda expression to a variable and reuse it
What is a lambda expression (Java)
[Kotlin] 3 ways to get Class from KClass
A program that calculates factorials from 2 to 100
Choose a class hierarchy from tagged classes
I want to ForEach an array with a Lambda expression in Java
I tried to automatically generate a class to convert from a data class to a Bundle with APT
GetInstance () from a @Singleton class in Groovy from Java
How to deploy a container on AWS Lambda
[Android] Inherit ImageView to create a new class
Connect to Aurora (MySQL) from a Java application
How to get a heapdump from a Docker container
To become a VB.net programmer from a Java shop
How to create a class that inherits class information
How to get Class from Element in Java
How to convert a solidity contract to a Java contract class
How to use Java API with lambda expression
Java8 to start now ~ forEach and lambda expression ~
Pass the condition to be used in the Java8 lambda expression filter () as a parameter
[Java] Lambda expression
Java lambda expression
Pass Form class from Controller to Thymeleaf by Spring-Boot
3. Create a database to access from the web module
I want to call a method of another class
How to jump from Eclipse Java to a SQL file
Extract elements in order from a class type ArrayList
How to deploy to Heroku from a local docker image
[Java] How to erase a specific character from a character string
Send a command to Spigot from an external process
[Ruby/Rails] How to generate a password in a regular expression
Item 25: Limit source files to a single top-level class
I want to connect to Heroku MySQL from a client
A Java user over a dozen years ago tried to study the functions of Java8 (Lambda expression).