Java8 method reference

Let's study Java 8 method references with you this time.

First of all, let's make an age sword method by the method before Java 8.

use lombok

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;

public class App {
    public static void main( String[] args ) {
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Jack",25));
        personList.add(new Person("Mary",23));
        personList.add(new Person("Jill",24));
        personList.add(new Person("Dock",34));
        personList.add(new Person("Lily",27));


        //Anonymous class
        Collections.sort(personList, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge() - o2.getAge();
            }
        });

        System.out.println(personList);
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Person {
    private String name;
    private int age;
}

Implementation results: [Person (name = Mary, age = 23), Person (name = Jill, age = 24), Person (name = Jack, age = 25), Person (name = Lily, age = 27), Person (name = Dock, age = 34)]

Lambda expression

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;

public class App {
    public static void main( String[] args ) {
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Jack",25));
        personList.add(new Person("Mary",23));
        personList.add(new Person("Jill",24));
        personList.add(new Person("Dock",34));
        personList.add(new Person("Lily",27));

        //Lambda expression
        Collections.sort(personList,(p1,p2) -> p1.getAge() - p2.getAge());
        System.out.println(personList);
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Person {
    private String name;
    private int age;
    
}

Implementation results: [Person (name = Mary, age = 23), Person (name = Jill, age = 24), Person (name = Jack, age = 25), Person (name = Lily, age = 27), Person (name = Dock, age = 34)]

static method reference

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;

public class App {
    public static void main( String[] args ) {
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Jack",25));
        personList.add(new Person("Mary",23));
        personList.add(new Person("Jill",24));
        personList.add(new Person("Dock",34));
        personList.add(new Person("Lily",27));

        //static method reference
        Collections.sort(personList, Person::CompareAge);
        System.out.println(personList);

    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Person {
    private String name;
    private int age;

    public static int CompareAge(Person o1, Person o2) {
        return o1.getAge() - o2.getAge();
    }
}


Recommended Posts

Java8 method reference
java8 method reference
Java method
java (method)
Java method
[Java] method
[Java] method
[Java] forEach method
JAVA reference materials
[Java] Random method
My Java reference
[Java] split method
JAVA DB connection method
Java learning 2 (learning calculation method)
Java learning memo (method)
[Java ~ Method ~] Study memo (5)
About method splitting (Java)
Studying Java 8 (see method)
Java programming (class method)
[Java] Basic method notes
[Java] Integer wrapper class reference
Method
Java VB.net service reference halfway
Java Silver Study Method Memo
Create a java method [Memo] [java11]
Java test code method collection
[Java Silver] About equals method
Java
[Java] Timer processing implementation method
[Java] Random number generation method (Random)
Java methods and method overloads
Java
Benefits of Java static method
[Java Silver] Array generation method
[Java] New Thread generation method (1)
[Java] Object-oriented syntax --class method / argument
Automatic photo resizing method in Java
Java method list (memorial) (under construction)
[Java] How to use join method
Screen transition by Post method [Java]
[Java] Object-oriented syntax-class / field / method / scope
Java primitive types, reference types, Immutable, Mutable
Java reference mechanism (stack and heap)
Java comparison using the compareTo () method
[Java beginner] == operator and equals method
Java array variables are reference types
Java pass by value and pass by reference
Try to extract java public method
[Java] Reference / update of Active Directory
About Java primitive types and reference types
Try using IBM Java method tracing
Java "pass by reference" problem summary
[Java] Proxy setting method when starting Java
Java basic data types and reference types
Java beginner design pattern (Factory Method pattern)
[Java] Processing time measurement method memo
Call the super method in Java
About returning a reference in a Java Getter
Java learning (0)
Implementation of clone method for Java Record
My thoughts on the equals method (Java)