Comparable和Comparator
Posted 千彧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Comparable和Comparator相关的知识,希望对你有一定的参考价值。
Comparable和Comparator接口都是用来实现集合中元素的比较,
- 只是Comparable是在集合内部定义的方法实现的排序,
- 想实现排序,就需要在集合外定义Comparator接口的方法 compareTo()
- Comparable位于包java.lang下
- 确定排序方式后就无法改变
编写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 }
- Comparator是在集合外部实现的排序,
- 想实现排序,就需要在集合内部实现Comparable的方法 compare() 另外还提供了equals()
- Comparator位于包java.util下
- 采用策略模式,不同情况下采取不同方式比较。
编写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的主要内容,如果未能解决你的问题,请参考以下文章