Comparable与Comparator的比较与使用
一 概述
1.Comparable与Comparator使用背景
数值型数据(byte int short long float double)天生可对比大小,可排序,String实现了Comparable接口也可以对比大小与排序,而自定义类多种多样,没有一个共有的可以用作排序的指标,因此需要在自定义类中手动建立对比的方法,出于这个目的,java提供了两个接口Comparable与Comparator。
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的比较与使用的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

在使用IntelliJIDEAUltimate版本启动Spring...

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

在使用TKMyBatis进行数据库查询时,如何优雅地获取实体类变量名以构建查询条件,是一个常见的难题。本文将针...

Redis缓存方案如何实现产品排行榜列表的需求?在开发过程中,我们常常需要处理排行榜的需求,例如展示一个�...
