java-函数式编程-方法引用
·
静态方法引用
类名::静态方法
使用场景
若某个Lambda表达式里只是调用一个静态方法,并且“->”前后参数的形式一致,就可以使用静态方法引用
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
//姓名,年龄,身高,性别
private String name;
private int age;
private double height;
private char sex;
public static int compareByAge(Student s1, Student s2) {
return s1.getAge() - s2.getAge();
}
}
public class demo {
public static void main(String[] args) {
//静态方法引用
test1();
}
public static void test1() {
Student[] students = new Student[6];
students[0] = new Student("小王", 18, 180.5, '男');
students[1] = new Student("小张", 25, 180.5, '男');
students[2] = new Student("小李", 17, 170.5, '男');
students[3] = new Student("小赵", 20, 165.5, '女');
students[4] = new Student("小王", 19, 182.5, '男');
students[5] = new Student("小周", 35, 170.5, '女');
//需求:按年龄升序排序,可以调用sun公司写好的API直接对数组进行排序
//Arrays.sort(students, (o1, o2) -> o1.getAge() - o2.getAge());
//Arrays.sort(students, (o1, o2) -> Student.compareByAge(o1, o2));
//静态方法引用,类名::静态方法
Arrays.sort(students, Student::compareByAge);
for (int i = 0; i < students.length; i++) {
Student s = students[i];
System.out.println(s);
}
}
}
实例方法引用
对象名::实例方法
使用场景
若某个Lambda表达式里只是通过对象名称调用一个实例方法,并且“->”前后参数的形式一致,就可以使用实例方法引用
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
//姓名,年龄,身高,性别
private String name;
private int age;
private double height;
private char sex;
public static int compareByAge(Student s1, Student s2) {
return s1.getAge() - s2.getAge();
}
//实例方法
public int compareByHeight(Student o1,Student o2) {
//按照身高比较
return Double.compare(o1.getHeight(), o2.getHeight());
}
}
public class demo2 {
public static void main(String[] args) {
//实例方法引用
test1();
}
public static void test1() {
Student[] students = new Student[6];
students[0] = new Student("小王", 18, 180.5, '男');
students[1] = new Student("小张", 25, 180.5, '男');
students[2] = new Student("小李", 17, 170.5, '男');
students[3] = new Student("小赵", 20, 165.5, '女');
students[4] = new Student("小王", 19, 182.5, '男');
students[5] = new Student("小周", 35, 170.5, '女');
Student t = new Student();
//Arrays.sort(students, (o1, o2) -> t.compareByHeight(o1, o2));
//实例方法引用,对象名::实例方法
//前提:->前后的参数一致,才能用实例方法引用
Arrays.sort(students, t::compareByHeight);
for (int i = 0; i < students.length; i++) {
Student s = students[i];
System.out.println(s);
}
}
}
特定类型方法的引用
特定类的类名::方法
使用场景
若某个Lambda表达式里只是调用一个特定类型的实例方法,并且前面参数列表中的第一个参数是作为方法的主调,后面的所有参数都是作为该实例方法的入参的,就可以使用特定类型的方法引用
public class demo3 {
public static void main(String[] args) {
//特定类型的方法引用
//需求:有一个字符串数组,里面有一些人的名字,按照名字的首字母升序排序
String[] names = {"tom","smith","jack","scott","king","mae","elysia","kiana","bronia","Ayaka","asuna"};
//把数组进行排序
//Arrays.sort(names);//默认按照首字母编号升序排列
//要求:忽略首字母大小进行升序排序(java官方搞不定,要自己制定规则)
/*Arrays.sort(names, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareToIgnoreCase(o2);//java已经封装了字符串按照首字母忽略大小写比较的方法
}
});*/
//Arrays.sort(names, (o1, o2) -> o1.compareToIgnoreCase(o2));
//特定类型方法引用,类名::方法名
Arrays.sort(names, String::compareToIgnoreCase);
System.out.println(Arrays.toString(names));
}
}
构造器引用
类名::new
使用场景
若某个Lambda表达式里只是在创建对象,并且“->”前后参数的形式一致,就可以使用构造器引用
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
public class demo4 {
public static void main(String[] args) {
//构造器引用
//创建了接口的匿名内部类对象
/*CarFactory cf = new CarFactory() {
@Override
public Car getCar(String name) {
return new Car(name);
}
};*/
//CarFactory cf = name -> new Car(name);
//构造器引用:类名::new
CarFactory cf = Car::new;
Car c1 = cf.getCar("保时捷");
System.out.println(c1);
}
}
@FunctionalInterface
interface CarFactory{
Car getCar(String name);
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Car{
private String name;
}
更多推荐
所有评论(0)