1、一个类在设计之初就要实现对该类对象的排序功能,那么这个类要实现Comparable接口,实现public int compareTo(T t)方法。如代码中的Student类。
对于实现Comparable接口的类,

调用java.util.Arrays.sort(Object[] a)对包含对象实例的数组进行排序,

调用java.util.Collections.sort(List<T> list)对包含对象实例的list进行排序。

2、若一个类在设计之初并没有对排序功能的需求,而是在后续的使用中想要对这个类添加排序的功能,如Teacher类。这时的办法是实现一个比较器类,
如TeacherComparator类,实现public int compare(T t1, T t2)让该类实现Comparator接口。

调用java.util.Arrays.sort(T[] a, Comparator<? super T> c) 对包含对象实例的数组进行排序,
调用java.util.Collections.sort(List<T> list, Comparator<? super T> c) 对包含对象实例的list进行排序。

3、a.compareTo(b)方法和compare(a,b)方法,在方法中判断两个对象的某个或某些属性的大小关系,若a的某个属性比b的属性小,返回负整数,若相等,返回0,否则返回正整数。

if a.attr < b.attr

return -1 

else 

return 1

上面即为按照某属性从小到大排序。

if a.attr < b.attr

return 1 

else 

return -1

上面为按照某属性从大到小排序。

以下代码按照Score从高到底排序,若Score相同,则按照age从小到大排序。

public class Student implements Comparable<Student>{

    private String name;
private int id;
private int score;
private int age; public Student(String name, int id, int score, int age) {
super();
this.name = name;
this.id = id;
this.score = score;
this.age = age;
} @Override
public int compareTo(Student s) {
if (this.score < s.getScore()) {
return 1;
} else if (this.score > s.getScore()) {
return -1;
} else {
if (this.age < s.getAge()) {
return -1;
} else {
return 1;
}
}
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public int getScore() {
return score;
} public void setScore(int score) {
this.score = score;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public static void main(String[] args) { System.out.println("use Interface Comparable");
System.out.println(); System.out.println("Array ");
System.out.println(); Student[] stu = {new Student("a", 1, 88, 18),
new Student("b", 2, 78, 16),
new Student("c", 3, 88, 17),
new Student("d", 4, 78, 16),
new Student("e", 5, 100, 18)}; Arrays.sort(stu);
for (Student s : stu) {
System.out.println(s.getScore() + " " + s.getAge());
} System.out.println("***");
System.out.println("List");
System.out.println(); List<Student> list = new ArrayList<Student>();
list.add(new Student("a", 1, 88, 18));
list.add(new Student("b", 2, 78, 16));
list.add(new Student("c", 3, 88, 17));
list.add(new Student("d", 4, 78, 16));
list.add(new Student("e", 5, 100, 18));
Collections.sort(list);
for (Student s : list) {
System.out.println(s.getScore() + " " + s.getAge());
}
System.out.println();
System.out.println("use Interface Comparator");
System.out.println();
System.out.println("Array");
System.out.println(); Teacher[] teach = {
new Teacher("a", 1, 88, 18),
new Teacher("b", 2, 78, 16),
new Teacher("c", 3, 88, 17),
new Teacher("d", 4, 78, 16),
new Teacher("e", 5, 100, 18)};
Arrays.sort(teach, new TeacherComparator());
for (Teacher t : teach) {
System.out.println(t.getScore() + " " + t.getAge());
} System.out.println();
System.out.println("List");
System.out.println(); List<Teacher> tList = new ArrayList<Teacher>();
tList.add(new Teacher("a", 1, 88, 18));
tList.add(new Teacher("b", 2, 78, 16));
tList.add(new Teacher("c", 3, 88, 17));
tList.add(new Teacher("d", 4, 78, 16));
tList.add(new Teacher("e", 5, 100, 18));
Collections.sort(tList, new TeacherComparator());
for (Teacher t : tList) {
System.out.println(t.getScore() + " " + t.getAge());
}
}
} class Teacher { private String name;
private int id;
private int score;
private int age; public Teacher(String name, int id, int score, int age) {
super();
this.name = name;
this.id = id;
this.score = score;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public int getScore() {
return score;
} public void setScore(int score) {
this.score = score;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
} class TeacherComparator implements Comparator<Teacher> { @Override
public int compare(Teacher t1, Teacher t2) { if (t1.getScore() < t2.getScore()) {
return 1;
} else if (t1.getScore() > t2.getScore()) {
return -1;
} else {
if (t1.getAge() < t2.getAge()) {
return -1;
} else {
return 1;
}
}
} }

Comparable接口和Comparator接口的更多相关文章

  1. Java中实现对象的比较:Comparable接口和Comparator接口

    在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...

  2. Java6.0中Comparable接口与Comparator接口详解

    Java6.0中Comparable接口与Comparator接口详解 说到现在,读者应该对Comparable接口有了大概的了解,但是为什么又要有一个Comparator接口呢?难道Java的开发者 ...

  3. Java:实现对象的比较 comparable接口和comparator接口

    在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...

  4. Java—集合框架 Collections.sort()、Comparable接口和Comparator接口

    Collentions工具类--java.util.Collections Collentions是Java集合框架中,用来操作集合对象的工具类,也是Java集合框架的成员,与List.Map和Set ...

  5. Comparable接口与Comparator接口的比较————总结

    之前的两篇文章主要学习了Comparable接口和Comparator接口的学习.既然已经学习完了,现在就趁热打铁,进行总结吧! Comparable接口和Comparator接口的共同点: 1. 都 ...

  6. 比较器:Compare接口与Comparator接口区别与理解

    一.实现Compare接口与Comparator接口的类,都是为了对象实例数组排序的方便,因为可以直接调用 java.util.Arrays.sort(对象数组名称),可以自定义排序规则. 不同之处: ...

  7. java中Comparatable接口和Comparator接口的区别

    1.不同类型的排序规则 .自然排序是什么?   自然排序是一种升序排序.对于不同的数据类型,升序规则不一样:   BigDecimal BigInteger Byte Double Float Int ...

  8. Comparatable接口和Comparator接口的使用与区别

    这篇博文可以为你解决的问题如下: 什么是自然排序 Collections.sort()与Arrays.sort()的异同点 Comparatable接口和Comparator接口各自的排序依据(理论讲 ...

  9. Java中的Comparable接口和Comparator接口

    Comparator位于包java.util下,比较器,是在集合外部定义排序.Comparable位于包java.lang下,代表当前对象可比较的,是在集合内部实现排序. Comparable代表一个 ...

  10. Java之Comparable接口和Comparator接口

    Comparable & Comparator 都是用来实现集合中元素的比较.排序的: Comparable 是在集合内部定义的方法实现的排序: Comparator 是在集合外部实现的排序: ...

随机推荐

  1. Linux查询一台机器的IP地址和其对应的域名

    Linux查询一台机器的IP地址和其对应的域名 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ nslookup > 127.0.0.1 Server: ...

  2. 异常-----freemarker.core.ParseException: Encountered "string"

    1.错误描述 freemarker.core.ParseException: Encountered "string" at line 21, column 21 in type. ...

  3. HttpServletResponse,HttpServletRequest详解

    1.相关的接口   HttpServletRequest HttpServletRequest接口最常用的方法就是获得请求中的参数,这些参数一般是客户端表单中的数据.同时,HttpServletReq ...

  4. SpringMVC源码情操陶冶-RequestMappingHandlerAdapter适配器

    承接前文SpringMVC源码情操陶冶-HandlerAdapter适配器简析.RequestMappingHandlerAdapter适配器组件是专门处理RequestMappingHandlerM ...

  5. 简述Spring事务有几种管理方法,写出一种配置方式

    Spring事务有两种方式: 1.编程式事务:(代码中嵌入) 2.声明式事务:(注解,XML) 注解方式配置事务的方式如下: 首先,需要在applicationContext.xml中添加启动配置,代 ...

  6. 【UVa11426】GCD - Extreme (II)(莫比乌斯反演)

    [UVa11426]GCD - Extreme (II)(莫比乌斯反演) 题面 Vjudge 题解 这.. 直接套路的莫比乌斯反演 我连式子都不想写了 默认推到这里把.. 然后把\(ans\)写一下 ...

  7. 如何使用mysqldump备份数据库

    一.背景 在开发项目中,数据库是核心资产.除了做主备冗余增加可靠性外,定期备份数据也是必须的. 使用mysqldump备份数据具有操作简单,备份和恢复时间短的优点(mysqldump备份数据生成的是批 ...

  8. 关于word2016中mathtype无法使用以及“由于宏安全设置,无法找到宏或宏已被禁用”的解决方案

    版本描述: 系统:win10 64位 word: 2016版 32位 Mathtype: 6.9d (6.9b也出现相同问题,应该可以通过相同的方法解决) 问题描述: 自从在一次win10更新之后,w ...

  9. MongoDB系列一(查询).

    一.简述 MongoDB中使用find来进行查询.查询就是返回一个集合中文档的子集,子集合的范围从0个文档到整个集合.默认情况下,"_id"这个键总是被返回,即便是没有指定要返回这 ...

  10. repo 和git的用法

    1. 服务器版本下载: repo init -u git@192.168.1.11:i700t_60501010/platform/manifest.git-b froyo_almond -m M76 ...