I'll write a commentary about Java lambda expressions.
In the first place, lambda expressions are often used in functional languages as a way to describe anonymous functions. It is used when creating a temporary function, such as when passing a function to a function.
There is no such thing as a function in Java (methods are always defined in a class). Therefore, Java lambda expressions behave as "create and pass an instance of an interface with a single abstract method". It is limited to a single method because a lambda expression can only describe one expression, and an interface with multiple methods does not determine which method's expression.
As a method of description
(argument) -> {processing}
Write. If you have only one argument or action, you can omit the parentheses.
As a concrete example, let's look at the sort
method of Collections
.
This is a method that sorts on the first argument List
, but if you pass an instance of the Comparator
interface as the second argument, the comparable
method will be used to determine the sort order.
For example, if you want to sort a list of strings (strList
) by character length, you can write an anonymous function as follows.
Collections.sort(strList, new Comparator(){
@Override
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
})
Since the Comparator
class is an interface in which only the compare
method ** ** is defined, it can be rewritten as follows with a lambda expression.
List<String> strList = Arrays.asList("a","ccc","bb");
Collections.sort(strList, (s1, s2) -> s1.length() - s2.length());
System.out.println(strList); // [a, bb, ccc]
The comparing
method is defined in the Comparator
class as follows.
comparing(Function<? super T,? extends U> keyExtractor);
The argument type is Function <? Super T ,? extends U>
, which is a type of ** functional interface **.
Functional interfaces are interfaces that can use lambda expressions, and various types are available.
This time, Function <? Super T,? Extends U>
indicates that the instance has a type with an argument of T and a return value of U.
T and U are defined as follows:
<T,U extends Comparable<? super U>> Comparator<T>
T is the type of the instance to be compared. U is the type of field for comparison. Therefore, this functional interface means that it is an expression that returns information U for comparing instances of T.
If you write the processing of Collections.sort
earlier with the comparing
method, it will be as follows.
Collections.sort(strList, comparing(s -> s.length()));
The s-> s.length ()
part means to call the length method of the String type instance s, so it can be rewritten with ** method reference **.
A method reference is a description method that makes it easy to write the process of calling a method of that class for an instance.
Describe with class name :: method name
.
If you write the previous example with a method reference, it will be as follows.
Collections.sort(strList, comparing(String::length));
reference (o1, o2)-> o1 --o2 Stop spells! --How to use Comparator with Java 8 JavaDoc:Comparator
Recommended Posts