[Java Silver] Summary of points related to lambda expressions

What is a lambda expression?

A mechanism to assign the code you want to execute to an interface type variable that has only one method that needs to be implemented.

What is a functional interface?

A functional interface is an interface that has only ** one method ** that needs to be implemented. It is called "functional" because there is only one output for each input.

Types of functional interfaces

Interface type Methods that need to be implemented Feature
Consumer<T> void accept(T) Receives and processes arguments. Arguments that do not return resultsconsumer
Supplier<T> T get() Return only the result without receiving anythingSupplier
Predicate<T> boolean test(T) Take an argument and evaluate itAssertive
Function<T, R> R apply(T) Receives an argument and the specified type(R)Returns the result ofProcessor

Functional interface example

Reference) Class for Java8 lambda expression

**-The forEach method of the java.util.List interface can receive a Consumer type lambda expression ** reference) Official Document Interface List Official Document forEach

Example)

public static void main(String[] args){
   List<Integer> list = List.of(1,2,3);
   list.forEach((x) ->{System.out.println(x)}); //Receives and processes arguments and does not return values
}

** · toUpperCase method ** of java.lang.String class can receive Function type lambda expression

reference Official document toUpperCase ()

Example)

public static void main(String[] args){
   Function<String, String> func = (x) -> {return x.toUpperCase();}; //Takes an argument and returns the result of the specified type
   System.out.println(func.apply("hello world"));
}

How to write a lambda expression


Basic

Function type interface type variable name = (argument)-> {// processing ** return **};

  Function<String, String> func = (x) -> { return x.toUpperCase(); }; 

<When there is one argument, () is omitted>

Functional interface type Variable name = Arguments-> {// Processing ** return **};

 Function<String, String> func = x -> { return x.toUpperCase(); }; 

<When there is only one process, {} is omitted>

Functional interface type Variable name = Arguments-> // Processing; ** * I can't write a return statement! !! ** **

list.forEach( x -> System.out.println(x) ); 

Lambda-style notes

** [The formal argument of a lambda expression is a constant or a substantially constant] **

-Compilation error example (from Java Silver Kuromoto)

void sample(){
  int i = 0;
  Supplier<Integer> foo = () -> i;    
 i++;   //i needs to be substantially constant
  System.out.println(foo.get());
}

** [Note the data type of the argument] ** Variable declarations in lambda expressions can ** omit specifying data types **. Because the type of the argument can be inferred from the type of the functional interface you are trying to assign.

Even if you write a lambda expression variable with its arguments omitted and the type omitted, it is a newly defined variable. → ** Check if it overlaps with an existing variable in the scope of the lambda expression! !! ** **

-Example of compilation error

final String val = "Hello World";
Consumer<String> func = (val) -> System.out.println(val); //Duplicate name caused by val of lambda expression and val of local variable, compilation error

** [Note the scope] ** The ** scope ** of a lambda expression extends to the entire block where the lambda expression is written.

-Example of compilation error

public class Main{
   public static void main(String[] args){
     Srting val = "A";  //Local variable val in scope of lambda expression
     Function f = (val) -> {  //Lambda expression variable val
       System.out.println(val);
     };
   }
}

Future tasks

To be honest, I still don't understand the Lambda style at all ... I would like to understand the following site about the mechanism of lambda expression.

Computer Science and Magic Y Combinator

Also, I don't really understand when to actually use a lambda expression. What is the method that can accept a lambda expression? Is it always available when writing simple processes that are not reused? Can it be used only by a method of a functional interface? How do you distinguish it from a method of a functional interface?

There are many things I don't understand.

Recommended Posts

[Java Silver] Summary of points related to lambda expressions
[Java] Summary of how to abbreviate lambda expressions
[Java Silver] Summary of access modifier points
[Java] Summary of regular expressions
[Java] Introduction to lambda expressions
Summary of knowledge required to pass Java SE8 Silver
[Introduction to Java] About lambda expressions
Summary of [Java silver study] package
The origin of Java lambda expressions
How to use Java lambda expressions
Summary of stumbling blocks related to form_with
I tried to summarize Java lambda expressions
[java] Summary of how to handle char
Java related summary
[Java] [Maven3] Summary of how to use Maven3
[java] Summary of how to handle character strings
Understand Java 8 lambda expressions
About Java lambda expressions
Explain Java 8 lambda expressions
Summary of Java support 2018
Summary of good points and precautions when converting Java Android application to Kotlin
Summary of points I was worried about when migrating from java to kotlin
Use Java lambda expressions outside of the Stream API
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
Summary of how to implement default arguments in Java
[Java11] Stream Summary -Advantages of Stream-
[Java] Summary of operators (operator)
Java8 stream, lambda expression summary
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
[Java Silver] Things to be aware of regarding switch statements
Java lambda expressions learned with Comparator
How to study Java Silver SE 8
[For beginners] Summary of java constructor
[Java] Points to note with Arrays.asList ()
About Lambda, Stream, LocalDate of Java8
Road to Java SE 11 Silver acquisition
[Java] Input to stdin of Process
AtCoder 400 points algorithm summary (Java edition)
How to pass Oracle Java Silver
Getting started with Java lambda expressions
Summary of object-oriented programming using Java
A little understanding of lambda expressions
Summary of in-house newcomer study session [Java]
How to make a Java calendar Summary
Name a group of regular expressions (Java)
Java SE8 Silver ~ The Road to Pass ~
How to use submit method (Java Silver)
Nowadays Java lambda expressions and Stream API
Summary of changes other than JEP of Java10
Summary of how to write annotation arguments
[Java] Personal summary of conditional statements (basic)
Summary of going to JJUG CCC 2019 Spring
List of recommended articles related to Corda