demo(class40): 函数式变成(静态方法引用、实例方法引用、特定类型方法引用)
- 新增 Demo1、Demo2、Demo3 和 Student 类 - 示例代码展示了使用静态方法引用、实例方法引用和 Lambda 表达式进行排序 - 包含按年龄和身高排序学生对象的逻辑 - 添加了字符串数组的排序示例
This commit is contained in:
21
src/class40/Demo1.java
Normal file
21
src/class40/Demo1.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package class40;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Demo1 {
|
||||
public static void main(String[] args) {
|
||||
Student[] students = new Student[6];
|
||||
students[0] = new Student("般素素", 35, 171.5, '女');
|
||||
students[1] = new Student("杨幂", 28, 168.5, '女');
|
||||
students[2] = new Student("张无忌", 25, 181.5, '男');
|
||||
students[3] = new Student("小昭", 19, 165.5, '女');
|
||||
students[4] = new Student("赵敏", 27, 167.5, '女');
|
||||
students[5] = new Student("刘亦菲", 36, 168, '女');
|
||||
|
||||
// Arrays.sort(students,(o1, o2)-> o1.getAge()-o2.getAge());
|
||||
//
|
||||
// Arrays.sort(students , ((o1, o2) -> Student.compareByAge(o1, o2)));
|
||||
|
||||
Arrays.sort(students, Student::compareByAge);//静态方法引用,简化lambda表达式
|
||||
}
|
||||
}
|
||||
23
src/class40/Demo2.java
Normal file
23
src/class40/Demo2.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package class40;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Demo2 {
|
||||
public static void main(String[] args) {
|
||||
Student[] students = new Student[6];
|
||||
students[0] = new Student("般素素", 35, 171.5, '女');
|
||||
students[1] = new Student("杨幂", 28, 168.5, '女');
|
||||
students[2] = new Student("张无忌", 25, 181.5, '男');
|
||||
students[3] = new Student("小昭", 19, 165.5, '女');
|
||||
students[4] = new Student("赵敏", 27, 167.5, '女');
|
||||
students[5] = new Student("刘亦菲", 36, 168, '女');
|
||||
|
||||
|
||||
Student t = new Student();
|
||||
Arrays.sort(students, Student::compareByAge);
|
||||
// Arrays.sort(students, ((o1, o2) -> t.compareByHeight(o1, o2)));
|
||||
Arrays.sort(students, (t::compareByHeight)); //实例方法引用
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
33
src/class40/Demo3.java
Normal file
33
src/class40/Demo3.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package class40;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class Demo3 {
|
||||
public static void main(String[] args) {
|
||||
//创建一个字符串数组,用来模拟一个手机通讯录(英文名)
|
||||
String[] names = {"Tom","Jerry","Bobi","曹操","Mike","angela","Dlei","Mary","Rose","Andy","caocao"};
|
||||
// 方法1
|
||||
Arrays.sort(names, new Comparator<String>() {
|
||||
@Override
|
||||
public int compare(String o1, String o2) {
|
||||
return o1.compareToIgnoreCase(o2);
|
||||
}
|
||||
}); //默认首字母排序,大写在前,小写字母在后,中文最后
|
||||
|
||||
// 方法2
|
||||
Arrays.sort(names, (o1, o2) -> o1.compareToIgnoreCase(o2));
|
||||
|
||||
|
||||
// 方法3
|
||||
Arrays.sort(names, String::compareToIgnoreCase);
|
||||
|
||||
|
||||
|
||||
|
||||
System.out.println(Arrays.toString(names));
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
22
src/class40/Student.java
Normal file
22
src/class40/Student.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package class40;
|
||||
|
||||
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 o1, Student o2){
|
||||
return o1.getAge() - o2.getAge();
|
||||
}
|
||||
public int compareByHeight(Student o1, Student o2){
|
||||
return Double.compare(o1.getHeight(), o2.getHeight());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user