java8 method reference

Preface

I only knew how to use forEach using a lambda expression, but there was also a method using the method reference "::", so make a note.

Method reference

Class name (instance) :: method name

You can refer to the method by writing as

forEach example

Method reference

list.forEach(System.out :: println);

Lambda expression

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

Example of use

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {

        Main main = new Main();
        System.out.println("----No1----");
        main.test().forEach(main :: myPrint);
        System.out.println("----No2----");
        main.test().forEach(x -> main.myPrint(x));
        
    }
    
    public List<String> test(){
        List<String> list = new ArrayList<>();
        list.add("AA");
        list.add("BB");
        list.add("CC");
        list.add("DD");
        return list;
    }
    
    public void myPrint(String str){
        System.out.println(str);
    }
}


This is an example of calling the method of the instance, but the execution result is the same for both.

Execution result

----No1----
AA
BB
CC
DD
----No2----
AA
BB
CC
DD

Recommended Posts

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 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] 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)
Studying Java ―― 3
[Java] array
Java protected