一、People类属性Comparable接口
(1)People类重写compareTo方法。
(2)调用Collections.sort()方法排序。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
public class test{
public static void main(String[] args) throws Exception {
People person1 = new People("Alice", 25, 1.65);
People person2 = new People("Bob", 30, 1.78);
People person3 = new People("Charlie", 22, 1.70);
People person4 = new People("David", 28, 1.75);
People person5 = new People("Eva", 35, 1.68);
People person6 = new People("Frank", 27, 1.80);
People person7 = new People("Grace", 31, 1.72);
People person8 = new People("Henry", 24, 1.68);
People person9 = new People("Ivy", 29, 1.76);
People person10 = new People("Jack", 33, 1.79);
Vector<People> ve = new Vector<>();
ArrayList<People> list = new ArrayList<>();
ve.add(person1);
ve.add(person2);
ve.add(person3);
ve.add(person4);
ve.add(person5);
ve.add(person6);
ve.add(person7);
ve.add(person8);
ve.add(person9);
ve.add(person10);
for (int i=0;i<10;i++){
list.add(ve.get(i));
}
System.out.println("排序前");
for(int i=0;i<10;i++){
System.out.println(ve.get(i));
}
System.out.println("-------------------");
//(1)方法一
Collections.sort(ve);
//Collections.sort(list);
System.out.println("排序后");
for(int i=0;i<10;i++){
System.out.println(ve.get(i));
}
}
}
//(1)方法一
class People implements Comparable<People>{
String name;
int age;
double heigh;
public People() {
}
public People(String name, int age, double heigh) {
this.name = name;
this.age = age;
this.heigh = heigh;
}
//升序
@Override
public int compareTo(People o) {
if(this.heigh-o.heigh>0){
return 1;
}else if(this.heigh-o.heigh<0){
return -1;
}
return 0;
}
//降序
// @Override
// public int compareTo(People o) {
// if(this.heigh-o.heigh>0){
// return -1;
// }else if(this.heigh-o.heigh<0){
// return 1;
// }
// return 0;
// }
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
", heigh=" + heigh +
'}';
}
}
二、Collections.sort()中第二参数new Comparator<People> 并重写compare方法
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
public class test{
public static void main(String[] args) throws Exception {
People person1 = new People("Alice", 25, 1.65);
People person2 = new People("Bob", 30, 1.78);
People person3 = new People("Charlie", 22, 1.70);
People person4 = new People("David", 28, 1.75);
People person5 = new People("Eva", 35, 1.68);
People person6 = new People("Frank", 27, 1.80);
People person7 = new People("Grace", 31, 1.72);
People person8 = new People("Henry", 24, 1.68);
People person9 = new People("Ivy", 29, 1.76);
People person10 = new People("Jack", 33, 1.79);
Vector<People> ve = new Vector<>();
ArrayList<People> list = new ArrayList<>();
ve.add(person1);
ve.add(person2);
ve.add(person3);
ve.add(person4);
ve.add(person5);
ve.add(person6);
ve.add(person7);
ve.add(person8);
ve.add(person9);
ve.add(person10);
for (int i=0;i<10;i++){
list.add(ve.get(i));
}
for(int i=0;i<10;i++){
System.out.println(ve.get(i));
}
System.out.println("-------------------");
//(2)方法二
Collections.sort(ve, new Comparator<People>() {
@Override
public int compare(People o1, People o2) {
if(o1.heigh-o2.heigh>0){
return 1;//升序
}else if(o1.heigh-o2.heigh<0){
return -1;//降序
}
return 0;
}
});
// Collections.sort(list, new Comparator<People>() {
// @Override
// public int compare(People o1, People o2) {
// if(o1.heigh-o2.heigh>0){
// return 1;
// }else if(o1.heigh-o2.heigh<0){
// return -1;
// }
// return 0;
// }
// });
for(int i=0;i<10;i++){
System.out.println(ve.get(i));
}
}
}
//(2)方法二
class People{
String name;
int age;
double heigh;
public People() {
}
public People(String name, int age, double heigh) {
this.name = name;
this.age = age;
this.heigh = heigh;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
", heigh=" + heigh +
'}';
}
}
提示:
以上比较身高是double类型,如果比较的是int类型重写的方法可以简化如下
1、重写方法
@Override
public int compareTo(People o) {
return this.age-o.age;//升序
//return o.age-this.age;//降序
}
@Override
public int compare(People o1, People o2) {
return o1.age-o2.age;//升序
//return o2.age-o1.age;//降序
}
2、Lambda表达式
//Lambda表达式简化后
Collections.sort(ve, (o1,o2) ->{ return o1.age-o2.age;});//升序
Collections.sort(ve, (o1,o2) ->{ return o2.age-o1.age;});//降序