【Java常用类库】_比较器(Comparable、Comparator)笔记

来源:互联网 发布:手机淘宝秒杀怎么刷新 编辑:程序博客网 时间:2024/05/17 05:58

【Java常用类库】_比较器(Comparable、Comparator)笔记

分类: 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:表示相等

实例代码:

[java] view plaincopyprint?
  1. class Student implements Comparable<Student>{  
  2.     private String name;  
  3.     private int age;  
  4.     private float score;  
  5.     public Student(String name,int age,float score){  
  6.         this.name = name;  
  7.         this.age = age;  
  8.         this.score = score;  
  9.     }  
  10.     public String toString(){  
  11.         return name+"\t\t"+this.age+"\t\t"+this.score;  
  12.     }  
  13.     public int compareTo(Student stu){  
  14.         if(this.score>stu.score){  
  15.             return -1;  
  16.         }else if(this.score<stu.score){  
  17.             return 1;  
  18.         }else{  
  19.             if(this.age>stu.age){  
  20.                 return 1;  
  21.             }else if(this.age<stu.age){  
  22.                 return -1;  
  23.             }else{  
  24.                 return 0;  
  25.             }  
  26.         }  
  27.     }  
  28. }  
  29. public class ComparableDemo01{  
  30.     public static void main(String args[]){  
  31.         Student stu[] = {new Student("张三",20,99.0f),new Student("李四",22,90.0f),new Student("王五",22,100.0f)};  
  32.         java.util.Arrays.sort(stu);    //进行排序操作  
  33.         for(int i=0;i<stu.length;i++){    //循环输出数组内容  
  34.             System.out.println(stu[i]);  
  35.         }  
  36.     }  
  37. }  



注意:如果在此时Student类中没有实现Comparable接口,则在执行时会出现以下的异常。

Exception in thread "main" java.lang.ClassCastException:

3.2、分析比较器的排序原理:


实际上之前所讲解的排序过程,也就是经常听到数据结构中的二叉树的排序方法,通过二叉树进行排序,之后利用中序遍历的方式把内容依次读取出来。

中序遍历首先遍历左子树,然后访问根结点,最后遍历右子树。在遍历左、右子树时,仍然先遍历左子树,再访问根结点,最后遍历右子树。



下面就自己手工实现一个二叉树的比较算法。
为了操作方法,此处使用Integer类完成。

[java] view plaincopyprint?
  1. public class ComparableDemo02{  
  2.     public static void main(String args[]){  
  3.         Comparable com = null ;            // 声明一个Comparable接口对象  
  4.         com = 30 ;                        // 通过Integer为Comparable实例化  
  5.         System.out.println("内容为:" + com) ;    // 调用的是toString()方法  
  6.     }  
  7. };  



了解了此特性之后,下面就可以动手完成一个二叉树算法。

[java] view plaincopyprint?
  1. class BinaryTree{  
  2.     class Node{            // 声明一个节点类  
  3.         private Comparable data ;    // 保存具体的内容  
  4.         private Node left ;            // 保存左子树  
  5.         private Node right ;        // 保存右子树  
  6.         public Node(Comparable data){  
  7.             this.data = data ;  
  8.         }  
  9.         public void addNode(Node newNode){  
  10.             // 确定是放在左子树还是右子树  
  11.             if(newNode.data.compareTo(this.data)<0){    // 内容小,放在左子树  
  12.                 if(this.left==null){  
  13.                     this.left = newNode ;    // 直接将新的节点设置成左子树  
  14.                 }else{  
  15.                     this.left.addNode(newNode) ;    // 继续向下判断  
  16.                 }  
  17.             }  
  18.             if(newNode.data.compareTo(this.data)>=0){    // 放在右子树  
  19.                 if(this.right==null){  
  20.                     this.right = newNode ;    // 没有右子树则将此节点设置成右子树  
  21.                 }else{  
  22.                     this.right.addNode(newNode) ;    // 继续向下判断  
  23.                 }  
  24.             }  
  25.         }  
  26.         public void printNode(){    // 输出的时候采用中序遍历  
  27.             if(this.left!=null){  
  28.                 this.left.printNode() ;    // 输出左子树  
  29.             }  
  30.             System.out.print(this.data + "\t") ;  
  31.             if(this.right!=null){  
  32.                 this.right.printNode() ;  
  33.             }  
  34.         }  
  35.     };  
  36.     private Node root ;        // 根元素  
  37.     public void add(Comparable data){    // 加入元素  
  38.         Node newNode = new Node(data) ;    // 定义新的节点  
  39.         if(root==null){    // 没有根节点  
  40.             root = newNode ;    // 第一个元素作为根节点  
  41.         }else{  
  42.             root.addNode(newNode) ; // 确定是放在左子树还是放在右子树  
  43.         }  
  44.     }  
  45.     public void print(){  
  46.         this.root.printNode() ;    // 通过根节点输出  
  47.     }  
  48. };  
  49. public class ComparableDemo03{  
  50.     public static void main(String args[]){  
  51.         BinaryTree bt = new BinaryTree() ;  
  52.         bt.add(8) ;  
  53.         bt.add(3) ;  
  54.         bt.add(3) ;  
  55.         bt.add(10) ;  
  56.         bt.add(9) ;  
  57.         bt.add(1) ;  
  58.         bt.add(5) ;  
  59.         bt.add(5) ;  
  60.         System.out.println("排序之后的结果:") ;  
  61.         bt.print() ;  
  62.     }  
  63. };  




3.3、另一种比较器:Comparator

直接贴出代码:

[java] view plaincopyprint?
  1. import java.util.* ;  
  2. class Student{    // 指定类型为Student  
  3.     private String name ;  
  4.     private int age ;  
  5.     public Student(String name,int age){  
  6.         this.name = name ;  
  7.         this.age = age ;  
  8.     }  
  9.     public boolean equals(Object obj){    // 覆写equals方法  
  10.         if(this==obj){  
  11.             return true ;  
  12.         }  
  13.         if(!(obj instanceof Student)){  
  14.             return false ;  
  15.         }  
  16.         Student stu = (Student) obj ;  
  17.         if(stu.name.equals(this.name)&&stu.age==this.age){  
  18.             return true ;  
  19.         }else{  
  20.             return false ;  
  21.         }  
  22.     }  
  23.     public void setName(String name){  
  24.         this.name = name ;  
  25.     }  
  26.     public void setAge(int age){  
  27.         this.age = age ;  
  28.     }  
  29.     public String getName(){  
  30.         return this.name ;  
  31.     }  
  32.     public int getAge(){  
  33.         return this.age ;  
  34.     }  
  35.     public String toString(){  
  36.         return name + "\t\t" + this.age  ;  
  37.     }  
  38. };  
  39.   
  40. class StudentComparator implements Comparator<Student>{    // 实现比较器  
  41.     // 因为Object类中本身已经有了equals()方法  
  42.     public int compare(Student s1,Student s2){  
  43.         if(s1.equals(s2)){  
  44.             return 0 ;  
  45.         }else if(s1.getAge()<s2.getAge()){    // 按年龄比较  
  46.             return 1 ;  
  47.         }else{  
  48.             return -1 ;  
  49.         }  
  50.     }  
  51. };  
  52.   
  53. public class ComparatorDemo{  
  54.     public static void main(String args[]){  
  55.         Student stu[] = {new Student("张三",20),  
  56.             new Student("李四",22),new Student("王五",20),  
  57.             new Student("赵六",20),new Student("孙七",22)} ;  
  58.         java.util.Arrays.sort(stu,new StudentComparator()) ;    // 进行排序操作  
  59.         for(int i=0;i<stu.length;i++){    // 循环输出数组中的内容  
  60.             System.out.println(stu[i]) ;  
  61.         }  
  62.     }  
  63. };  
0 0
原创粉丝点击