Detailed reference method for lambda expressions

background

lambda expressions now, has appeared in many languages, the syntax is simple, powerful, able to just a single line of code on several lines arrived before jdk1.8, lambda expressions in general has been a lot written data, This article does not describe, we have to discuss today is one of the more difficult to understand the expression to read: ::

Is the expression of two colons, String :: new, System.out :: println ... and so on, to write very big on this, but under what circumstances you can use this expression, how we write it? This is the purpose of this article ~

lambda expression

First is the conclusion: lambda wording divided into a total of 4, respectively,

1, class static methods ::

This is well understood, it should be noted that such an approach with the class. Static method call is not the same, or that there is no relationship, this is a call to (specific use) method, and we said today It is a reference method, similar to the "pointer" is pointing method only.

We compare the students' grades, for example, the code Description:

public class Student {

    private int age;

    public Student(int age) {
        this.age = age;
    }

    public Student() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Then use a tool to define the method of comparison rules

{class CompareUtil public 

    public static int min (A int, int B) { 
        return A - B; 
    } 

    // deliberately write the 
    public int max (A int, int B) { 
        return B - A; 
    } 
}

The first test start writing, sort of the student's age

static void main public (String [] args) { 

    List <Student> Al = Arrays.asList ( 
            new new Student (10), 
            new new Student (24), 
            new new Student (14), 
            new new Student (. 4) 
    ); 
    al.sort ( CompareUtil :: min); // first written expression [class] :: static method 
    al.forEach (A-> System.out.println (a.getAge ())); 
}

got the answer

2, an example method Examples ::

In this case, the format is just need to pass the lambda expression in line with an instance method of a class, we call common method for testing

public static void main(String[] args) {

        List<Student> al = Arrays.asList(
                new Student(10),
                new Student(24),
                new Student(14),
                new Student(4)
        );
        //实例方法,首先创建出实例
        CompareUtil compare = new CompareUtil();
        al.sort(compare::max);
//        al.sort(CompareUtil::min);
        al.forEach(a -> System.out.println(a.getAge()));
    }

got the answer

3, the class instance method ::

In this case, special in that depends entirely on the requirements of the call, in lambda expressions, the first parameter as an example of the method call, and therefore requires a first class parameters which are representative of this method, then the other parameters of the lambda expression as an argument to the called method ; as understood from the above indirect: a first parameter called instance method, the instance is provided which defines a method, which is similar to the second mode: example: : example method, but examples using the first parameter lambda, as in the Student class, plus a comparison of the method, the following examples

public class Student {

    private int age;

    public Student(int age) {
        this.age = age;
    }

    public Student() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int max(Student p) {
        return -this.getAge() + p.getAge();
    }

Then we can use the

static void main public (String [] args) { 

        List <Student> Al = Arrays.asList ( 
                new new Student (10), 
                new new Student (24), 
                new new Student (14), 
                new new Student (. 4) 
        ); 
        // instance method , first create an instance 
// = new new CompareUtil CompareUtil Compare (); 
// al.sort (Compare :: max); 
// al.sort (CompareUtil :: max); // compile error, al.sort inside lambda student first parameter, apparently no student class method max 
// al.sort ((s1, s2) -> s1.getAge () - s2.getAge ()); // this is the original lambda expression, so the first is a parameter Student 
        al.sort (Student :: max); 
// al.sort (CompareUtil :: min); 
        al.forEach (a -> System.out.println (a.getAge ())); 
    }

4, constructor

This is easier to understand, in fact, is the use of the function interface supplier, the type is () -> T, representative of the input parameters is empty, then a return output; this is not the null constructor parameters do ~

public String mixString(Supplier<String> a) {
    return a.get() + "mix";
}

transfer

MethodRef2 methodRef2 = new MethodRef2();
System.out.println(methodRef2.mixString(String::new));

If the constructor that takes a parameter, then the method to represent the parameters of the line

System.out.println(methodRef2.mixString2("abc", String::new));

Although it looks the same form, but we can see the new click to call a String constructor with parameters, we can try.

These are the four kinds of writing and usage scenarios, which look at scala or lambda expression jdk8 source will not feel the feeling ignorant of the force. . .

 

Published 12 original articles · won praise 1 · views 360

Guess you like

Origin blog.csdn.net/weixin_39800596/article/details/104430880