Java lambda expressions learned with Comparator

I'll write a commentary about Java lambda expressions.

Java lambda expression

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(){
    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]

Functional interfaces and method references

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

Java lambda expressions learned with Comparator
Getting started with Java lambda expressions
Understand Java 8 lambda expressions
About Java lambda expressions
Explain Java 8 lambda expressions
[Java] Introduction to lambda expressions
[Introduction to Java] About lambda expressions
Handle exceptions coolly with Java 8 lambda expressions and Stream API
Rethinking design patterns with Java8 lambda expressions & Stream --Builder pattern -
What I learned with Java Gold
What I learned with Java Silver
The origin of Java lambda expressions
How to use Java lambda expressions
I tried to summarize Java lambda expressions
Nowadays Java lambda expressions and Stream API
AWS Lambda with Java starting now Part 1
Working with huge JSON in Java Lambda
Easy to trip with Java regular expressions
Hello Java Lambda
[Java] Lambda expression
Java lambda expression
Interact with LINE Message API using Lambda (Java)
[Java] Summary of how to abbreviate lambda expressions
How to use Java framework with AWS Lambda! ??
How to use Java API with lambda expression
Install java with Homebrew
Sorting using java comparator
Change seats with java
Install Java with Ansible
How to deploy Java to AWS Lambda with Serverless Framework
[Java Silver] Summary of points related to lambda expressions
[Java] Sort the list using streams and lambda expressions
java neutral lambda expression 1
Quarkus saves Java Lambda! ??
Use Java lambda expressions outside of the Stream API
Comfortable download with JAVA
Java lambda expression variations
Switch java with direnv
Java 8 lambda expression Feature
java lambda expression memo
Download Java with Ansible
Java lambda expression [memo]
Let's scrape with Java! !!
Studying Java 8 (lambda expression)
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Review java8 ~ Lambda expression ~
Java lambda expression again
Build Java with Wercker
Java Lambda Command Pattern
Endian conversion with JAVA
Regularly post imaged tweets on Twitter with AWS Lambda + Java
Have fun programming with lambda expressions and a fluid interface
Java (Kotlin / JVM) lambda expressions are not always separate instances
Create a SlackBot with AWS lambda & API Gateway in Java
Java multi-project creation with Gradle
Getting Started with Java Collection
Java Config with Spring MVC
Basic Authentication with Java 11 HttpClient
Let's experiment with Java inlining
Run batch with docker-compose with Java batch
[Template] MySQL connection with Java