码迷,mamicode.com
首页 > 其他好文 > 详细

Comparable和Comparator

时间:2017-10-27 13:35:35      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:this   比较   反向   return   com   sys   []   策略   lan   

Comparable和Comparator接口都是用来实现集合中元素的比较,

 

  1. 只是Comparable是在集合内部定义的方法实现的排序,
  2. 想实现排序,就需要在集合外定义Comparator接口的方法   compareTo()
  3. Comparable位于包java.lang下
  4. 确定排序方式后就无法改变

编写Student类实现接口Comparable:

 1 package org.comparable;
 2 /**
 3  * implements Comparable<Student>
 4  *这里泛型需要指定传入comparaTo传入的参数类型
 5  */
 6 public class Student implements Comparable<Student>{
 7 public Student(String name, Integer age) {
 8         super();
 9         this.name = name;
10         this.age = age;
11     }
12 
13 private String name;
14 private Integer age;
15     public String getName() {
16     return name;
17 }
18 public void setName(String name) {
19     this.name = name;
20 }
21 public Integer getAge() {
22     return age;
23 }
24 public void setAge(Integer age) {
25     this.age = age;
26 }
27 
28 @Override
29 public String toString() {
30     return "Student [name=" + name + ", age=" + age + "]";
31 }
32 /**
33  * 实现comparaTo方法的
34  */
35     public int compareTo(Student stu) {
36         //内部可以定义以name 为依据或以 age为依据 比较
37         //这里的  age需要使用Integer类型,因为int没有实现comparable接口,而String Integer等是实现了的
38 //        return this.getAge().compareTo(stu.getAge());
39         return stu.getAge().compareTo(this.getAge());
40 //        return this.getName().compareTo(stu.getName());
41 //        return stu.getName().compareTo(this.getName());
42     }
43 
44 }

测试类:

 1 package org.comparable;
 2 import java.util.Arrays;
 3 public class ComparableTest {
 4     public static void main(String[] args) {
 5 Student[] allStudents = new Student[4];
 6 allStudents[0] = new Student("a", 1);
 7 allStudents[1] = new Student("b", 2);
 8 allStudents[2] = new Student("c", 3);
 9 allStudents[3] = new Student("d", 4);
10 //排序
11 Arrays.sort(allStudents);
12 for (int i = 0; i < allStudents.length; i++) {
13     System.out.println(allStudents[i].toString());
14 }
15 
16     }
17 
18 }

 

 

  1. Comparator是在集合外部实现的排序,
  2. 想实现排序,就需要在集合内部实现Comparable的方法          compare()  另外还提供了equals()
  3. Comparator位于包java.util下
  4. 采用策略模式,不同情况下采取不同方式比较。

编写Student不实现接口Comparator:

 1 package org.comparator;
 2 
 3 import java.util.Comparator;
 4 /**
 5  * 这里并未实现 Comparator接口
 6  * @author Administrator
 7  *
 8  */
 9 public class Student {
10 public Student(Integer age, String name) {
11         super();
12         this.age = age;
13         this.name = name;
14     }
15 
16 private Integer age;
17 private String name; 
18 @Override
19 public String toString() {
20     return "Student [age=" + age + ", name=" + name + "]";
21 }
22 
23 
24 public int getAge() {
25     return age;
26 }
27 
28 
29 public void setAge(Integer age) {
30     this.age = age;
31 }
32 
33 
34 public String getName() {
35     return name;
36 }
37 
38 
39 public void setName(String name) {
40     this.name = name;
41 }
42 
43 }

 

测试类:

 1 package org.comparator;
 2 
 3 import java.util.Arrays;
 4 import java.util.Comparator;
 5 
 6 import org.comparable.Student;
 7 
 8 public class ComparatorTest {
 9 
10     public static void main(String[] args) {
11         Student[] allStudents = new Student[4];
12         allStudents[0] = new Student("a", 1);
13         allStudents[1] = new Student("b", 2);
14         allStudents[2] = new Student("c", 3);
15         allStudents[3] = new Student("d", 4);
16         
17 /**
18  * 调用外部定义排序方式排序        
19  */
20 //Arrays.sort(allStudents, new StudentSortWithAge());
21         
22         
23 /**
24  * 可以选择 在使用时在进行重写  编写匿名类
25  */
26 
27 Arrays.sort(allStudents,new Comparator<Student>(){
28             public int compare(Student s1, Student s2) {
29                 //定义反向 , 从大到小 排 
30                 if (s1.getAge()>s2.getAge()) {
31                     return -1;
32                 }else if (s1.getAge()<s2.getAge()) {
33                     return 1;
34                 }else {
35                     return 0;
36                 }
37             }
38             
39         });
40         for (int i = 0; i < allStudents.length; i++) {
41             System.out.println(allStudents[i].toString());
42         }
43     }
44 }

 

Comparable和Comparator

标签:this   比较   反向   return   com   sys   []   策略   lan   

原文地址:http://www.cnblogs.com/the-wang/p/7741933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!