静态方法引用
import lombok.AllArgsConstructor;
import lombok.Data;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
import java.util.zip.DeflaterOutputStream;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/8 23:35
*/
@Data
@AllArgsConstructor
class Student{
int age;
double height;
}
public class LambdaDemo {
public static void main(String[] args) {
Student[] students = new Student[2];
students[0] = new Student(11, 180.9);
students[1] = new Student(19, 180.6);
/*
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge();
}
});
*/
/*
Arrays.sort(students, (o1, o2)-> o1.getAge() - o2.getAge());
*/
//Arrays.sort(students, (o1, o2)-> CompareByAge.compareByAge(o1,o2));
Arrays.sort(students, CompareByAge::compareByAge);
}
}
class CompareByAge{
static int compareByAge(Student o1, Student o2){
return o1.getAge() - o2.getAge();
}
}
实例方法引用
import lombok.AllArgsConstructor;
import lombok.Data;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
import java.util.zip.DeflaterOutputStream;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/8 23:35
*/
@Data
@AllArgsConstructor
class Student{
int age;
double height;
}
public class LambdaDemo {
public static void main(String[] args) {
Student[] students = new Student[2];
students[0] = new Student(11, 180.9);
students[1] = new Student(19, 180.6);
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return 0;
}
});
//实现年龄降序排序从
//Arrays.sort(students, (o1,o2)-> o2.getAge() - o1.getAge());
/*
final CompareByAge compareByAge = new CompareByAge();
Arrays.sort(students, (o1,o2)-> compareByAge.compareByAgeDesCheck(o1,o2));
*/
/*
final CompareByAge compareByAge = new CompareByAge();
Arrays.sort(students, (o1,o2)-> compareByAge.compareByAgeDesCheck(o1,o2));
*/
final CompareByAge compareByAge = new CompareByAge();
Arrays.sort(students, compareByAge::compareByAgeDesCheck);
}
}
class CompareByAge{
public static int compareByAge(Student o1, Student o2){
return o1.getAge() - o2.getAge();
}
public int compareByAgeDesCheck(Student o1, Student o2){
return o2.getAge() - o1.getAge();
}
}
特定类型的方法引用
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Arrays;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/8 23:35
*/
@Data
@AllArgsConstructor
class Student{
int age;
double height;
}
public class LambdaDemo {
public static void main(String[] args) {
String[] names = new String[]{"boby", "angela", "Andy", "dlei", "caocao", "Babo", "jack", "Cici"};
// Arrays.sort(names, new Comparator<String>() {
// @Override
// public int compare(String o1, String o2) {
// return o1.compareToIgnoreCase(o2);
// }
// });
// Arrays.sort(names, (o1, o2)->o1.compareToIgnoreCase(o2));
//如果某个Lambda表达式只是调用一个实例方法,并且前面参数列表中的第一个参数是作为方法的主调,后面的所有参数都是作为该实例方法的入参
//此时就可以使用特定类型的方法引用
Arrays.sort(names, String::compareToIgnoreCase);
System.out.println(Arrays.toString(names));
}
}
构造器引用
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Arrays;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/8 23:35
*/
@Data
@AllArgsConstructor
class Student{
int age;
double height;
}
public class LambdaDemo {
public static void main(String[] args) {
// CreateCar cc = new CreateCar(){
// @Override
// public Car create(String name, Double price) {
// return new Car(name, price);
// }
// };
//如果某个Lambda表达式李只是在创建对象,并且前后参数情况一致,可以使用构造器引用。
// CreateCar cc = (String name, Double price)-> new Car(name, price);
CreateCar cc = Car::new;
final Car car = cc.create("Pickle",49.9);
System.out.println(car);
}
}
@FunctionalInterface
interface CreateCar{
Car create(String name, Double price);
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class Car{
private String name;
private Double prices;
}
标签:Arrays,Java8,简化,Student,new,import,o2,o1,Lambda
From: https://www.cnblogs.com/poteitoutou/p/18062112