Java Lambda 表达式以及对 Lambda 表达式的简化

Java Lambda 表达式以及对 Lambda 表达式的简化

LambdaDemo.java

package com.zhong.lambdademo;

import java.util.Arrays;
import java.util.Comparator;

public class LambdaDemo {
    public static void main(String[] args) {

        Student[] student = new Student[5];
        student[0] = new Student("小钟", 179.1, 22);
        student[1] = new Student("小王", 152.1, 21);
        student[2] = new Student("小张", 199.2, 32);
        student[3] = new Student("小红", 120.8, 92);
        student[4] = new Student("小芳", 169.3, 54);

        // 1.原始排序
        Arrays.sort(student, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                // return o2.age-o1.age;   降序
                return o1.getAge() - o2.getAge();
            }
        });

        // lambda 表达式
        Arrays.sort(student, (o1, o2) -> o2.getAge() - o1.getAge());

        // 静态方法引用
        Arrays.sort(student, CompareByData::CompareByAge);

        // 实例方法引用
        CompareByData compareByData = new CompareByData();
        Arrays.sort(student, compareByData::CompareByAge2);

        for (Student stu : student) {
            System.out.println(stu);
        }

        String[] names = {"body", "angela", "Andy", "deli", "caoCao", "BaBo", "jack", "jk"};
        // 忽略首字符大小写排序
        Arrays.sort(names, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareToIgnoreCase(o2);
            }
        });
        // 特定类型方法引用简化
        Arrays.sort(names, String::compareToIgnoreCase);
        System.out.println(Arrays.toString(names));

        // 构造器引用
        CreateStudent createStudent = new CreateStudent() {
            @Override
            public Student Create(String name, Double height, int age) {
                return new Student(name, height, age);
            }
        };
        Student createStu = createStudent.Create("小钟", 179.1, 22);
        System.out.println(createStu);

        // lambda 简化
        CreateStudent createStudent1 = (name, height, age) -> new Student(name, height, age);
        Student createStu1 = createStudent1.Create("小王", 152.1, 21);
        System.out.println(createStu1);

        // 构造器引用简化
        CreateStudent createStudent2=  Student::new;
        Student createStu2 = createStudent2.Create("小红", 120.8, 92);
        System.out.println(createStu2);
    }
}

interface CreateStudent {
    Student Create(String name, Double height, int age);
}

CompareByData.java

package com.zhong.lambdademo;

public class CompareByData {
    public static int CompareByAge(Student o1, Student o2) {
        return o2.getAge() - o1.getAge();
    }

    public int CompareByAge2(Student o1, Student o2) {
        return o2.getAge() - o1.getAge();
    }
}

Student .java

package com.zhong.lambdademo;

public class Student {
    private String name;
    private Double height;
    private int age;

    public Student() {
    }

    public Student(String name, Double height, int age) {
        this.name = name;
        this.height = height;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getHeight() {
        return height;
    }

    public void setHeight(Double height) {
        this.height = height;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", height=" + height +
                ", age=" + age +
                '}';
    }
}

你可能感兴趣的:(Java,java,python,开发语言)