Comparable与Comparator接口

  从上面的两个接口看Collections.sort()与Arrays.sort()的两种用法是这篇文章的主要目的。

  Collections.sort()是集合的排序,在使用此方法的时候有两种途径可供我们选择:一种是要排序的集合中的成员实现Comparable接口,并重写conpareTo();另一种是实现自己的排序类(实现Comparator),并作为参数传给sort()方法。示意如下:

         方法1:   class Student implenents Comparable{compareTo()}//要排序的类

             ArrayList list = new ArrayList().add(new Student());//将上诉类装到集合中

             Collection.sort(list);//调用Collection的sort()方法进行排序

      方法2:     class Student{}//要排序的类

             MyComparator implenents Comparator{compare()}//实现自己的排序类

             ArrayList list = new ArrayList().add(new Student());//将上诉类装到集合中

             Collection.sort(list,new MyComparator());//调用Collection的sort()方法进行排序,并将自己的排序类作为参数传递

  Arrays.sort()是对数组尽心排序,排序的途径与Collection类似。一种是要排序的集合中的成员实现Comparable接口,并重写conpareTo();另一种是实现自己的排序类(实现Comparator),并作为参数传给sort()方法。示意如下:

         方法1:   class Student implenents Comparable{compareTo()}//要排序的类

             Student[] student = {new Student()}//此时不是装到集合中,而是装到数组中

             Arrays.sort(list);//调用Arrays的sort()方法进行排序

      方法2:     class Student{}//要排序的类

             MyComparator implenents Comparator{compare()}//实现自己的排序类

             Student[] student = {new Student()}//此时不是装到集合中,而是装到数组中

             Arrays.sort(list,new MyComparator());//调用Arrays的sort()方法进行排序,并将自己的排序类作为参数传递

  从上面的抽象出的过程可以看出,Collections与Arrays的排序过程是一样的,只不过是将Collection换做Arrays而已。

  下面给出类的目录结构图:Comparable与Comparator接口,上面的Comparable是测试Comparable按案例,下面的Comparator是测试Comparator的案例,两种案例中分别给出了集合和数组的排序。下面给出具体的测试代码,按照上图中的从上到下的类顺序:

 1 package Comparable;

 2 

 3 import java.util.Arrays;

 4 

 5 import org.junit.Test;

 6 

 7 public class ArraysTest {

 8     @Test

 9     public void test(){

10         Student stu1 = new Student("alsa", 21);

11         Student stu3 = new Student("foas", 17);

12         Student stu4 = new Student("mike", 25);

13         Student stu6 = new Student("zieka", 17);

14         Student stu2 = new Student("dava", 24);

15         Student stu5 = new Student("nose", 14);

16         

17         Student[] student = {stu1,stu2,stu3,stu4,stu5,stu6};

18         

19         System.out.println("排序前");

20         for(int i = 0;i<student.length;i++){

21             System.out.println(student[i]);

22         }

23         

24         Arrays.sort(student);

25         

26         System.out.println("排序后");

27         for(int i = 0;i<student.length;i++){

28             System.out.println(student[i]);

29         }

30     }

31 }
ArraysTest
 1 package Comparable;

 2 

 3 import java.util.ArrayList;

 4 import java.util.Collections;

 5 

 6 import org.junit.Test;

 7 

 8 

 9 

10 public class CollectionTest {

11     @Test

12     public void test(){

13         //这里都用英文名字是为了看字符串的排序

14         Student stu1 = new Student("alsa", 21);

15         Student stu3 = new Student("foas", 17);

16         Student stu4 = new Student("mike", 25);

17         Student stu6 = new Student("zieka", 17);

18         Student stu2 = new Student("dava", 24);

19         Student stu5 = new Student("nose", 14);

20         

21         ArrayList<Student> list = new ArrayList<Student>();

22         list.add(stu1);

23         list.add(stu2);

24         list.add(stu3);

25         list.add(stu4);

26         list.add(stu5);

27         list.add(stu6);

28         System.out.println("排序前");

29         for(Student l:list){

30             System.out.println(l);

31         }

32         System.out.println("排序后");

33         Collections.sort(list);

34         for(Student l:list){

35             System.out.println(l);

36         }

37     }

38     

39     

40 }
CollectionTest
 1 package Comparable;

 2 

 3 class Student  implements Comparable<Object>{

 4     String name;

 5     int age;

 6     

 7     public Student(String name, int age) {

 8         super();

 9         this.name = name;

10         this.age = age;

11     }

12     

13     public String getName() {

14         return name;

15     }

16     

17     public void setName(String name) {

18         this.name = name;

19     }

20     

21     public int getAge() {

22         return age;

23     }

24     

25     public void setAge(int age) {

26         this.age = age;

27     }

28 

29     @Override

30     public String toString() {

31         return "Student [name=" + name + ", age=" + age + "]";

32     }

33 

34     @Override

35     public int compareTo(Object o) {

36         // TODO Auto-generated method stub

37         if(((Student)o).age>age){

38             return 1;

39         }else if(((Student)o).age<age){

40             return -1;

41         }else{

42             return ((Student)o).name.compareTo(name);

43         }

44             

45     }

46     

47 }
Student
 1 package ComparatorTest;

 2 

 3 import java.util.Arrays;

 4 import java.util.Comparator;

 5 

 6 import org.junit.Test;

 7 

 8 class MyComparator1 implements Comparator<Object>{

 9 

10     @Override

11     public int compare(Object o1, Object o2) {

12         // TODO Auto-generated method stub

13         if(((Student)o1).age>((Student)o2).age){

14             return 1;

15         }else if(((Student)o1).age<((Student)o2).age){

16             return -1;

17         }else{

18             return ((Student)o1).name.compareTo(((Student)o2).name);

19         }

20     }

21     

22 }

23 

24 public class ArraysTest {

25     @SuppressWarnings("unchecked")

26     @Test

27     public void test(){

28         Student stu1 = new Student("alsa", 21);

29         Student stu3 = new Student("foas", 17);

30         Student stu4 = new Student("mike", 25);

31         Student stu6 = new Student("zieka", 17);

32         Student stu2 = new Student("dava", 24);

33         Student stu5 = new Student("nose", 14);

34         

35         Student[] student = {stu1,stu2,stu3,stu4,stu5,stu6};

36         System.out.println("排序前");

37         for(int i = 0;i<student.length;i++){

38             System.out.println(student[i]);

39         }

40         Arrays.sort(student,new MyComparator1());

41         System.out.println("排序后");

42         for(int i = 0;i<student.length;i++){

43             System.out.println(student[i]);

44         }

45     }

46 }
ArraysTest
 1 package ComparatorTest;

 2 

 3 

 4 import java.util.ArrayList;

 5 import java.util.Collections;

 6 import java.util.Comparator;

 7 

 8 import org.junit.Test;

 9 

10 @SuppressWarnings("rawtypes")

11 class MyComparator implements Comparator{

12 

13     @Override

14     public int compare(Object o1, Object o2) {

15         // TODO Auto-generated method stub

16         if(((Student)o1).age>((Student)o2).age){

17             return 1;

18         }else if(((Student)o1).age<((Student)o2).age){

19             return -1;

20         }else{

21             return ((Student)o1).name.compareTo(((Student)o2).name);

22         }

23     }

24     

25 }

26 

27 public class CollectionTest {

28     @SuppressWarnings("unchecked")

29     @Test

30     public void test(){

31         //这里都用英文名字是为了看字符串的排序

32         Student stu1 = new Student("alsa", 21);

33         Student stu3 = new Student("foas", 17);

34         Student stu4 = new Student("mike", 25);

35         Student stu6 = new Student("zieka", 17);

36         Student stu2 = new Student("dava", 24);

37         Student stu5 = new Student("nose", 14);

38         

39         ArrayList<Student> list = new ArrayList<Student>();

40         list.add(stu1);

41         list.add(stu2);

42         list.add(stu3);

43         list.add(stu4);

44         list.add(stu5);

45         list.add(stu6);

46         System.out.println("排序前");

47         for(Student l:list){

48             System.out.println(l);

49         }

50         System.out.println("排序后");

51         Collections.sort(list, new MyComparator());

52         for(Student l:list){

53             System.out.println(l);

54         }

55     }

56     

57     

58 }
CollectionTest
 1 package ComparatorTest;

 2 

 3 public class Student {

 4     String name;

 5     int age;

 6     

 7     public Student(String name, int age) {

 8         super();

 9         this.name = name;

10         this.age = age;

11     }

12     

13     public String getName() {

14         return name;

15     }

16     

17     public void setName(String name) {

18         this.name = name;

19     }

20     

21     public int getAge() {

22         return age;

23     }

24     

25     public void setAge(int age) {

26         this.age = age;

27     }

28 

29     @Override

30     public String toString() {

31         return "Student [name=" + name + ", age=" + age + "]";

32     }

33 

34 }
Student

 

你可能感兴趣的:(comparator)