本文主要是介绍对集合中的对象进行排序分别实现Comparable和comparator接口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
public class Driver {private String name;private int age;private int driveAge;//驾龄public Driver(String name, int age, int driveAge) {super();this.name = name;this.age = age;this.driveAge = driveAge;}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;}public int getDriveAge() {return driveAge;}public void setDriveAge(int driveAge) {this.driveAge = driveAge;}@Overridepublic String toString() {return "name=" + name + ", age=" + age + ", driveAge=" + driveAge;}}
public class Car implements Comparable<Car>{private String band;//品牌private double topSpeed;//最高速度private double price;//价格private Driver driver;//司机public Car(String band, double topSpeed, double price, Driver driver) {super();this.band = band;this.topSpeed = topSpeed;this.price = price;this.driver = driver;}public String getBand() {return band;}public void setBand(String band) {this.band = band;}public double getTopSpeed() {return topSpeed;}public void setTopSpeed(double topSpeed) {this.topSpeed = topSpeed;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public Driver getDriver() {return driver;}public void setDriver(Driver driver) {this.driver = driver;}@Overridepublic String toString() {return "band=" + band + ", topSpeed=" + topSpeed + ", price=" + price + ", driver=" + driver ;}@Overridepublic int compareTo(Car o) {int x=(int) (o.getTopSpeed()-this.getTopSpeed());return x;}}
public class CarDriverComparator implements Comparator<Car>{@Overridepublic int compare(Car o1, Car o2) {// TODO Auto-generated method stub//驾龄比较int num1=o2.getDriver().getDriveAge()-o1.getDriver().getDriveAge();//最大速度int num2=(int) (num1==0?o2.getTopSpeed()-o1.getTopSpeed():num1);return num2;}}
public class CarTest {public static void main(String[] args) {List<Car> list=new ArrayList<Car>();//创建4个司机对象Driver d1=new Driver("aa",23,12);Driver d2=new Driver("bb",24,10);Driver d3=new Driver("cc",25,12);Driver d4=new Driver("dd",26,17);//创建4个Car对象Car c1=new Car("宝马",120,120000,d1);Car c2=new Car("林肯",200,200000,d2);Car c3=new Car("红旗",100,100000,d3);Car c4=new Car("五菱",90,70000,d4);//添加到集合中list.add(c1);list.add(c2);list.add(c3);list.add(c4);//遍历输出System.out.println("输出集合元素:");Iterator<Car> c=list.iterator();while(c.hasNext()){System.out.println(c.next());}//comparable排序System.out.println("comparable排序(按照速度从大到小排序):");Collections.sort(list);for(Car x:list){System.out.println(x);}//comparator排序System.out.println("comparator排序(按照驾龄从大到小排序,如果驾龄相同,速度从大到小排序):");Collections.sort(list,new CarDriverComparator());for(Car x:list){System.out.println(x);}}
}
结果:
输出集合元素:
band=宝马, topSpeed=120.0, price=120000.0, driver=name=aa, age=23, driveAge=12
band=林肯, topSpeed=200.0, price=200000.0, driver=name=bb, age=24, driveAge=10
band=红旗, topSpeed=100.0, price=100000.0, driver=name=cc, age=25, driveAge=12
band=五菱, topSpeed=90.0, price=70000.0, driver=name=dd, age=26, driveAge=17
comparable排序(按照速度从大到小排序):
band=林肯, topSpeed=200.0, price=200000.0, driver=name=bb, age=24, driveAge=10
band=宝马, topSpeed=120.0, price=120000.0, driver=name=aa, age=23, driveAge=12
band=红旗, topSpeed=100.0, price=100000.0, driver=name=cc, age=25, driveAge=12
band=五菱, topSpeed=90.0, price=70000.0, driver=name=dd, age=26, driveAge=17
comparator排序(按照驾龄从大到小排序,如果驾龄相同,速度从大到小排序):
band=五菱, topSpeed=90.0, price=70000.0, driver=name=dd, age=26, driveAge=17
band=宝马, topSpeed=120.0, price=120000.0, driver=name=aa, age=23, driveAge=12
band=红旗, topSpeed=100.0, price=100000.0, driver=name=cc, age=25, driveAge=12
band=林肯, topSpeed=200.0, price=200000.0, driver=name=bb, age=24, driveAge=10
这篇关于对集合中的对象进行排序分别实现Comparable和comparator接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!