分类: Java
【Java常用类库】_比较器(Comparable、Comparator)笔记
本章目标:
掌握Comparable比较接口的使用
了解比较器的基本排序原理
掌握Comparator比较接口的使用
Comparable接口
可以直接使用java.util.Arrays类进行数组的排序操作,但对象所在的类必须实现Comparable接口,用于指定排序接口。
Comparable接口定义类如下:
public interface Comparable<T>{
public int compareTo(T o);
}
此方法返回一个int类型的数据,但是此int的值只能是以下三种:
1:表示大于
-1:表示小于
0:表示相等
实例代码:
注意:如果在此时Student类中没有实现Comparable接口,则在执行时会出现以下的异常。
Exception in thread "main" java.lang.ClassCastException:
实际上之前所讲解的排序过程,也就是经常听到数据结构中的二叉树的排序方法,通过二叉树进行排序,之后利用中序遍历的方式把内容依次读取出来。
下面就自己手工实现一个二叉树的比较算法。
为了操作方法,此处使用Integer类完成。
了解了此特性之后,下面就可以动手完成一个二叉树算法。
3.3、另一种比较器:Comparator
直接贴出代码:
本章目标:
掌握Comparable比较接口的使用
了解比较器的基本排序原理
掌握Comparator比较接口的使用
Comparable接口
可以直接使用java.util.Arrays类进行数组的排序操作,但对象所在的类必须实现Comparable接口,用于指定排序接口。
Comparable接口定义类如下:
public interface Comparable<T>{
public int compareTo(T o);
}
此方法返回一个int类型的数据,但是此int的值只能是以下三种:
1:表示大于
-1:表示小于
0:表示相等
实例代码:
- class Student implements Comparable<Student>{
- private String name;
- private int age;
- private float score;
- public Student(String name,int age,float score){
- this.name = name;
- this.age = age;
- this.score = score;
- }
- public String toString(){
- return name+"\t\t"+this.age+"\t\t"+this.score;
- }
- public int compareTo(Student stu){
- if(this.score>stu.score){
- return -1;
- }else if(this.score<stu.score){
- return 1;
- }else{
- if(this.age>stu.age){
- return 1;
- }else if(this.age<stu.age){
- return -1;
- }else{
- return 0;
- }
- }
- }
- }
- public class ComparableDemo01{
- public static void main(String args[]){
- Student stu[] = {new Student("张三",20,99.0f),new Student("李四",22,90.0f),new Student("王五",22,100.0f)};
- java.util.Arrays.sort(stu); //进行排序操作
- for(int i=0;i<stu.length;i++){ //循环输出数组内容
- System.out.println(stu[i]);
- }
- }
- }
注意:如果在此时Student类中没有实现Comparable接口,则在执行时会出现以下的异常。
Exception in thread "main" java.lang.ClassCastException:
3.2、分析比较器的排序原理:
实际上之前所讲解的排序过程,也就是经常听到数据结构中的二叉树的排序方法,通过二叉树进行排序,之后利用中序遍历的方式把内容依次读取出来。
中序遍历首先遍历左子树,然后访问根结点,最后遍历右子树。在遍历左、右子树时,仍然先遍历左子树,再访问根结点,最后遍历右子树。
下面就自己手工实现一个二叉树的比较算法。
为了操作方法,此处使用Integer类完成。
- public class ComparableDemo02{
- public static void main(String args[]){
- Comparable com = null ; // 声明一个Comparable接口对象
- com = 30 ; // 通过Integer为Comparable实例化
- System.out.println("内容为:" + com) ; // 调用的是toString()方法
- }
- };
了解了此特性之后,下面就可以动手完成一个二叉树算法。
- class BinaryTree{
- class Node{ // 声明一个节点类
- private Comparable data ; // 保存具体的内容
- private Node left ; // 保存左子树
- private Node right ; // 保存右子树
- public Node(Comparable data){
- this.data = data ;
- }
- public void addNode(Node newNode){
- // 确定是放在左子树还是右子树
- if(newNode.data.compareTo(this.data)<0){ // 内容小,放在左子树
- if(this.left==null){
- this.left = newNode ; // 直接将新的节点设置成左子树
- }else{
- this.left.addNode(newNode) ; // 继续向下判断
- }
- }
- if(newNode.data.compareTo(this.data)>=0){ // 放在右子树
- if(this.right==null){
- this.right = newNode ; // 没有右子树则将此节点设置成右子树
- }else{
- this.right.addNode(newNode) ; // 继续向下判断
- }
- }
- }
- public void printNode(){ // 输出的时候采用中序遍历
- if(this.left!=null){
- this.left.printNode() ; // 输出左子树
- }
- System.out.print(this.data + "\t") ;
- if(this.right!=null){
- this.right.printNode() ;
- }
- }
- };
- private Node root ; // 根元素
- public void add(Comparable data){ // 加入元素
- Node newNode = new Node(data) ; // 定义新的节点
- if(root==null){ // 没有根节点
- root = newNode ; // 第一个元素作为根节点
- }else{
- root.addNode(newNode) ; // 确定是放在左子树还是放在右子树
- }
- }
- public void print(){
- this.root.printNode() ; // 通过根节点输出
- }
- };
- public class ComparableDemo03{
- public static void main(String args[]){
- BinaryTree bt = new BinaryTree() ;
- bt.add(8) ;
- bt.add(3) ;
- bt.add(3) ;
- bt.add(10) ;
- bt.add(9) ;
- bt.add(1) ;
- bt.add(5) ;
- bt.add(5) ;
- System.out.println("排序之后的结果:") ;
- bt.print() ;
- }
- };
3.3、另一种比较器:Comparator
直接贴出代码:
- import java.util.* ;
- class Student{ // 指定类型为Student
- private String name ;
- private int age ;
- public Student(String name,int age){
- this.name = name ;
- this.age = age ;
- }
- public boolean equals(Object obj){ // 覆写equals方法
- if(this==obj){
- return true ;
- }
- if(!(obj instanceof Student)){
- return false ;
- }
- Student stu = (Student) obj ;
- if(stu.name.equals(this.name)&&stu.age==this.age){
- return true ;
- }else{
- return false ;
- }
- }
- public void setName(String name){
- this.name = name ;
- }
- public void setAge(int age){
- this.age = age ;
- }
- public String getName(){
- return this.name ;
- }
- public int getAge(){
- return this.age ;
- }
- public String toString(){
- return name + "\t\t" + this.age ;
- }
- };
- class StudentComparator implements Comparator<Student>{ // 实现比较器
- // 因为Object类中本身已经有了equals()方法
- public int compare(Student s1,Student s2){
- if(s1.equals(s2)){
- return 0 ;
- }else if(s1.getAge()<s2.getAge()){ // 按年龄比较
- return 1 ;
- }else{
- return -1 ;
- }
- }
- };
- public class ComparatorDemo{
- public static void main(String args[]){
- Student stu[] = {new Student("张三",20),
- new Student("李四",22),new Student("王五",20),
- new Student("赵六",20),new Student("孙七",22)} ;
- java.util.Arrays.sort(stu,new StudentComparator()) ; // 进行排序操作
- for(int i=0;i<stu.length;i++){ // 循环输出数组中的内容
- System.out.println(stu[i]) ;
- }
- }
- };