CompareByDate类
public class CompareByDate {
public static int compareByAge(Student o1,Student o2){
return o1.getAge() - o2.getAge();
}
}
Test类
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
Student[] students = new Student[4];
students[0] = new Student("张三",175.6,23);
students[1] = new Student("李四",169,20);
students[2] = new Student("王五",180.3,18);
students[3] = new Student("赵六",165.9,25);
// 原始写法,对数组中的学生对象按照年龄进行排序
// Arrays.sort(students, new Comparator() {
// @Override
// public int compare(Student o1, Student o2) {
// return o1.getAge() - o2.getAge();
// }
// });
// 使用Lambda简化后的形式
//Arrays.sort(students,(o1,o2) -> o1.getAge() - o2.getAge());
//Arrays.sort(students,(o1,o2) -> CompareByDate.compareByAge(o1,o2));
//静态方法引用
Arrays.sort(students,CompareByDate::compareByAge);
System.out.println(students);
}
}
CompareByDate类
public class CompareByDate {
public int compareByAgeDesc(Student o1,Student o2){
return o1.getAge() - o2.getAge();
}
}
Test类
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
Student[] students = new Student[4];
students[0] = new Student("张三",175.6,23);
students[1] = new Student("李四",169,20);
students[2] = new Student("王五",180.3,18);
students[3] = new Student("赵六",165.9,25);
// 原始写法,对数组中的学生对象按照年龄进行排序
// Arrays.sort(students, new Comparator() {
// @Override
// public int compare(Student o1, Student o2) {
// return o1.getAge() - o2.getAge();
// }
// });
// 使用Lambda简化后的形式
//Arrays.sort(students,(o1,o2) -> o1.getAge() - o2.getAge());
//Arrays.sort(students,(o1,o2) -> CompareByDate.compareByAge(o1,o2));
//实例方法引用
CompareByDate compare = new CompareByDate();
//Arrays.sort(students,((o1, o2) -> compare.compareByAgeDesc(o1,o2)));
Arrays.sort(students,compare::compareByAgeDesc); // 降序
System.out.println(students);
}
}
import java.util.Arrays;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
String[] names = {"boby","angela","Andy","dlei","caocao","Babo","jack","Cici"};
// 进行排序(默认是按照字符串的首字符编号进行升序排序的)
//Arrays。sort(names);
// 要求忽略首字符大小写进行排序
// Arrays.sort(names, new Comparator() {
// @Override
// public int compare(String o1, String o2) {
// // 制定比较规则
// return o1.compareToIgnoreCase(o2);
// }
// });
//Arrays.sort(names, ( o1, o2) -> o1.compareToIgnoreCase(o2));
Arrays.sort(names, String::compareToIgnoreCase);
System.out.println(Arrays.toString(names));
}
}
Student类
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Test类
public class Test {
public static void main(String[] args) {
// addStudent ss = new addStudent(){
// @Override
// public Student addstudent(String name, int age) {
// return new Student(name,age);
// }
// };
//addStudent ss = ( name, age) -> new Student(name,age);
//构造器引用
addStudent ss = Student::new;
Student s = ss.addstudent("张三",18);
System.out.println(s);
}
}
interface addStudent{
Student addstudent(String name,int age);
}