Comparable和Comparator接口

简介:

1、Comparable接口, 自身实现比较功能,只需要传入1个对象与自己进行比较

 
  1. class Student implements Comparable<Student> {  // 指定类型为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){  // 覆写compareTo()方法,实现排序规则的应用 
  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,90.0f), 
  32.             new Student("李四",22,90.0f),new Student("王五",20,99.0f), 
  33.             new Student("赵六",20,70.0f),new Student("孙七",22,100.0f)} ; 
  34.         java.util.Arrays.sort(stu) ;    // 进行排序操作 
  35.         for(int i=0;i<stu.length;i++){  // 循环输出数组中的内容 
  36.             System.out.println(stu[i]) ; 
  37.         } 
  38.     } 
  39. }; 

2、利用Comparable进行二叉树排序

 
  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、Comparator接口, 使用第三方来进行比较功能,需要传入2个对象

 
  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. }; 

 

 本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/417949,如需转载请自行联系原作者

相关文章
|
3月前
|
Java
Java中的比较器Comparable与Comparator
Java中的比较器Comparable与Comparator
|
4月前
|
Java
Comparable和Comparator两种比较器详解
Comparable和Comparator两种比较器详解
19 0
|
8月前
Comparable与Comparator对象比较
Comparable与Comparator对象比较
30 0
Comparable 和 Comparator的理解
Comparable是一个排序接口 此接口给实现类提供了一个排序的方法,此接口有且只有一个方法
95 0
Comparable 和 Comparator的理解
排序Comparable 和 Comparator的区别
排序Comparable 和 Comparator的区别
|
搜索推荐 算法 安全
浅谈Comparable和Comparator
首先我们考虑一个场景:有一个整形数组, 我们希望通过调用一个工具类的排序方法就能对该数组进行排序. 请看下面的代码:
Comparable与Comparator有什么区别?
Comparable与Comparator有什么区别?
177 0
关于Comparator和Comparable的理解
关于Comparator和Comparable的理解
87 0
|
Java
Comparable接口和Comparator接口
Comparable接口和Comparator接口
183 0
Comparable接口和Comparator接口