Demo(对象数组排序)
Student[] students = {new Student("张一", 20), new Student("张一", 18), new Student("张一", 30), new Student("张一", 17)};
Arrays.sort(students,(s1,s2)->s1.getAge()-s2.getAge()); // 按年龄从小到大排序
System.out.println(Arrays.tostring(students))
Student类
package Lambda闭包;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
详细介绍
http://www.importnew.com/16436.html
网友评论