java 两种比较器的详细介绍及实例

1. Comparator 和 Comparable 相同的地方

他们都是java的一个接口, 并且是用来对自定义的class比较大小的,

什么是自定义class: 如 public class Person{ String name; int age }.

当我们有这么一个personList,里面包含了person1, person2, persion3....., 我们用Collections.sort( personList ),
是得不到预期的结果的. 这时肯定有人要问, 那为什么可以排序一个字符串list呢:

如 StringList{"hello1" , "hello3" , "hello2"}, Collections.sort( stringList ) 能够得到正确的排序, 那是因为
String 这个对象已经帮我们实现了 Comparable接口 , 所以我们的 Person 如果想排序, 也要实现一个比较器。

2. Comparator 和 Comparable 的区别

Comparable

Comparable 定义在 Person类的内部:

public class Persion implements Comparable {..比较Person的大小..},

因为已经实现了比较器,那么我们的Person现在是一个可以比较大小的对象了,它的比较功能和String完全一样,可以随时随地的拿来
比较大小,因为Person现在自身就是有大小之分的。Collections.sort(personList)可以得到正确的结果。

Comparator

Comparator 是定义在Person的外部的, 此时我们的Person类的结构不需要有任何变化,如

public class Person{ String name; int age },

然后我们另外定义一个比较器:

public PersonComparator implements Comparator() {..比较Person的大小..},

在PersonComparator里面实现了怎么比较两个Person的大小. 所以,用这种方法,当我们要对一个 personList进行排序的时候,
我们除了了要传递personList过去, 还需要把PersonComparator传递过去,因为怎么比较Person的大小是在PersonComparator
里面实现的, 如:

Collections.sort( personList , new PersonComparator() ).


实例:

  1. package com.kk.Collection;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.Comparator;  
  6. import java.util.List;  
  7.   
  8. public class CompareTest {  
  9.     public static void main(String[] args) {  
  10.         List<Student> list=new ArrayList<Student>(10);  
  11.         list.add(new Student(1,"jj"));  
  12.         list.add(new Student(0,"ww"));  
  13.         list.add(new Student(0,"kk"));  
  14.         list.add(new Student(2,"ll"));  
  15.         Collections.sort(list); //内部比较器:要排序的对象实现Comparable接口,可以对自身进行比较  
  16.         System.out.println(list);  
  17.           
  18.         List<Teacher> t=new ArrayList<Teacher>(10);  
  19.         t.add(new Teacher(1,12));  
  20.         t.add(new Teacher(0,13));  
  21.         t.add(new Teacher(0,14));  
  22.         t.add(new Teacher(2,15));  
  23.         Collections.sort(t,new StudentComparator()); //外部比较器:通过实现Comparator接口  
  24.         System.out.println(t);  
  25.     }  
  26. }  
  27.   
  28. class Student implements Comparable {  
  29.     int num;  
  30.   
  31.     String name;  
  32.   
  33.     public Student(int num, String name) {  
  34.         this.num = num;  
  35.         this.name = name;  
  36.     }  
  37.       
  38.     @Override  
  39.     public String toString() {  
  40.         return "\r\tnum:"+num+" name:"+name+"\r";  
  41.     }  
  42.   
  43.     public int compareTo(Object o) {  
  44.         Student tmp=(Student) o;  
  45.         int result=tmp.num>num?1:(tmp.num==num?0:-1);  
  46.         if (result==0) {  
  47.             result=tmp.name.indexOf(0)>name.indexOf(0)?1:-1;  
  48.         }  
  49.         return result;  
  50.     }  
  51. }  
  52.   
  53. class Teacher{  
  54.     int num;  
  55.   
  56.     double salary;  
  57.       
  58.     public Teacher(int num, double salary) {  
  59.         this.num = num;  
  60.         this.salary = salary;  
  61.     }  
  62.   
  63.     @Override  
  64.     public String toString() {  
  65.         return "\r\tnum:"+num+" salary:"+salary+"\r";  
  66.     }      
  67. }  
  68.   
  69. class StudentComparator implements Comparator{  
  70.   
  71.     public int compare(Object o1, Object o2) {  
  72.         Teacher t1=(Teacher) o1;  
  73.         Teacher t2=(Teacher) o2;  
  74.         int result=t1.num>t2.num?1:(t1.num==t2.num?0:-1);  
  75.         return result=result==0?(t1.salary<t2.salary?1:-1):result;  
  76.     }  
  77.       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值