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.
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.
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 |
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
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"));
}
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) );
** [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);
};
}
}
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