2.集合排序collections.sort()底层排序依靠的是arrays.sort(),而arrays.sort()排序时采用的是冒泡法。
二 comparable需要对比大小的对象可以实现comparable接口,实现其中的抽象方法,该抽象方法用来设定比较的方式。下面以一个示例进行说明:
1.实体类package com.javase.collections.comparable;public class student implements comparable<student> {private string name;private int score;public student() {super(); }public student(string name, int score) {super();this.name = name;this.score = score; }public string getname() {return name; }public void setname(string name) {this.name = name; }public int getscore() {return score; }public void setscore(int score) {this.score = score; } @overridepublic int compareto(student stu) {return this.score - stu.score;// 操作对象减去参数对象,升序排列,反之降序。 } }
在compareto()方法中,以属性score为排序指标,采用“this.score-stu.score”,最终结果以升序排列,反之降序。
2.测试类package com.javase.collections.comparable;import java.util.arraylist;import java.util.collections;import java.util.list;import org.junit.test;public class comparabletest { @testpublic void testcomparable() { list<student> stus = new arraylist<student>(); student zhangsan = new student(zhangsan, 100); student lisi = new student(lisi, 90); student wanger = new student(wanger, 95); stus.add(zhangsan); stus.add(lisi); stus.add(wanger); system.out.println(排序前);for (student x : stus) { system.out.println(x.getname() + :: + x.getscore()); } system.out.println(排序后); collections.sort(stus);for (student x : stus) { system.out.println(x.getname() + :: + x.getscore()); } } }
输出:
三 comparator如果一个类在创建时未实现comparable接口,希望在不修改源码的情况下对其对象进行排序,可以在调用排序方法时实现comparator比较器接口,指定排序方法。下面以一个示例进行说明:
1.实体类package com.javase.collections.comparator;public class student {private string name;private int score;public student() {super(); }public student(string name, int score) {super();this.name = name;this.score = score; }public string getname() {return name; }public void setname(string name) {this.name = name; }public int getscore() {return score; }public void setscore(int score) {this.score = score; } }
2.测试类package com.javase.collections.comparator;import java.util.arraylist;import java.util.collections;import java.util.comparator;import java.util.list;import org.junit.test;public class comparatortest { @testpublic void test() { list<student> stus = new arraylist<student>(); student zhangsan = new student(zhangsan, 100); student lisi = new student(lisi, 90); student wanger = new student(wanger, 95); stus.add(zhangsan); stus.add(lisi); stus.add(wanger); system.out.println(排序前);for (student x : stus) { system.out.println(x.getname() + :: + x.getscore()); } system.out.println(-----------------------); collections.sort(stus, new comparator<student>() { @overridepublic int compare(student stu01, student stu02) {// return stu01.getscore() - stu02.getscore();//升序return stu02.getscore() - stu01.getscore();// 降序 } }); system.out.println(排序后);for (student x : stus) { system.out.println(x.getname() + :: + x.getscore()); } } }
在compare(student stu01, student stu02)方法中,以属性score为排序指标,采用“stu01.score-stu02.score”,最终结果升序排列,反之降序。
输出:
以上就是comparable与comparator的比较与使用的详细内容。
