zoukankan      html  css  js  c++  java
  • java比较器

    先来一个简单的实现

    这里实现 Comparable 接口,重写compareTo方法实现排序,当两个对象进行比较时,返回0代表它们相等;返回值<0,代表this排在被比较对象之前;反之代表在被比较对象之后

     1 package com.sun.comparator;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Arrays;
     5 import java.util.Collections;
     6 import java.util.List;
     7 
     8 public class ComparatorDemo implements Comparable<ComparatorDemo> {
     9 
    10     private String name;
    11     private Integer age;
    12     private float score;
    13 
    14     public ComparatorDemo(String name, Integer age, float score) {
    15         super();
    16         this.name = name;
    17         this.age = age;
    18         this.score = score;
    19     }
    20 
    21     @Override
    22     public String toString() {
    23         return "name:" + name + ";age:" + age + ";score:" + score;
    24     }
    25 
    26     @Override
    27     public int compareTo(ComparatorDemo o) {
    28         if (this.score > o.score)// score是private的,为什么能够直接调用,这是因为在Student类内部
    29             return -1;// 由高到底排序
    30         else if (this.score < o.score)
    31             return 1;
    32         else {
    33             if (this.age > o.age)
    34                 return 1;// 由底到高排序
    35             else if (this.age < o.age)
    36                 return -1;
    37             else
    38                 return 0;
    39         }
    40     }
    41 
    42     public static void main(String[] args) {
    43         List<ComparatorDemo> lists = new ArrayList<>();
    44         lists.add(new ComparatorDemo("a", 4, 4));
    45         lists.add(new ComparatorDemo("b", 3, 6));
    46         lists.add(new ComparatorDemo("c", 2, 3));
    47         lists.add(new ComparatorDemo("d", 1, 7));
    48         Collections.sort(lists);
    49         for (ComparatorDemo comparatorDemo : lists) {
    50             System.out.println("++++++" + comparatorDemo);
    51         }
    52         
    53         ComparatorDemo com[] = {new ComparatorDemo("a", 4, 4),
    54                 new ComparatorDemo("b", 3, 6),
    55                 new ComparatorDemo("c", 2, 3),
    56                 new ComparatorDemo("d", 1, 7)};
    57         Arrays.sort(com);
    58         for (ComparatorDemo comparatorDemo : com) {
    59             System.out.println("------" + comparatorDemo);
    60         }
    61     }
    62     
    63     public String getName() {
    64         return name;
    65     }
    66 
    67     public void setName(String name) {
    68         this.name = name;
    69     }
    70 
    71     public Integer getAge() {
    72         return age;
    73     }
    74 
    75     public void setAge(Integer age) {
    76         this.age = age;
    77     }
    78 
    79     public float getScore() {
    80         return score;
    81     }
    82 
    83     public void setScore(float score) {
    84         this.score = score;
    85     }
    86 
    87 }

     另一种方式

     1 package com.sun.comparator;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Arrays;
     5 import java.util.Collections;
     6 import java.util.Comparator;
     7 import java.util.List;
     8 
     9 public class ComparatorDemo2 {
    10 
    11     private String name;
    12     private Integer age;
    13     private float score;
    14 
    15     public ComparatorDemo2(String name, Integer age, float score) {
    16         super();
    17         this.name = name;
    18         this.age = age;
    19         this.score = score;
    20     }
    21 
    22     @Override
    23     public String toString() {
    24         return "name:" + name + ";age:" + age + ";score:" + score;
    25     }
    26 
    27 
    28     @SuppressWarnings({ "rawtypes", "unchecked" })
    29     public static void main(String[] args) {
    30         List<ComparatorDemo2> lists = new ArrayList<>();
    31         lists.add(new ComparatorDemo2("a", 4, 4));
    32         lists.add(new ComparatorDemo2("b", 3, 6));
    33         lists.add(new ComparatorDemo2("c", 2, 3));
    34         lists.add(new ComparatorDemo2("d", 1, 7));
    35         Comparator c = new ComparatorList();
    36         Collections.sort(lists, c);
    37         for (ComparatorDemo2 comparatorDemo : lists) {
    38             System.out.println("++++++" + comparatorDemo);
    39         }
    40         
    41         ComparatorDemo2 com[] = {new ComparatorDemo2("a", 4, 4),
    42                 new ComparatorDemo2("b", 3, 6),
    43                 new ComparatorDemo2("c", 2, 3),
    44                 new ComparatorDemo2("d", 1, 7)};
    45         Arrays.sort(com, c);
    46         for (ComparatorDemo2 comparatorDemo : com) {
    47             System.out.println("------" + comparatorDemo);
    48         }
    49     }
    50     
    51     public String getName() {
    52         return name;
    53     }
    54 
    55     public void setName(String name) {
    56         this.name = name;
    57     }
    58 
    59     public Integer getAge() {
    60         return age;
    61     }
    62 
    63     public void setAge(Integer age) {
    64         this.age = age;
    65     }
    66 
    67     public float getScore() {
    68         return score;
    69     }
    70 
    71     public void setScore(float score) {
    72         this.score = score;
    73     }
    74 
    75 }
     1 package com.sun.comparator;
     2 
     3 import java.util.Comparator;
     4 
     5 public class ComparatorList  implements Comparator<ComparatorDemo2>{
     6     //我们也可以通过构造方法注入一些参数加入到比较中
     7     
     8     @Override
     9     public int compare(ComparatorDemo2 o1, ComparatorDemo2 o2) {
    10         if (o1.getScore() > o2.getScore())
    11             return -1;// 由高到底排序
    12         else if (o1.getScore() < o2.getScore())
    13             return 1;
    14         else {
    15             if (o1.getAge() > o2.getScore())
    16                 return 1;// 由底到高排序
    17             else if (o1.getAge() < o2.getAge())
    18                 return -1;
    19             else
    20                 return 0;
    21         }
    22     }
    23 
    24 }

     下面是多个属性联合排序,注意,这里使用BeanComparator实现

     1 package com.sun.comparator;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Collections;
     5 import java.util.Comparator;
     6 
     7 import org.apache.commons.beanutils.BeanComparator;
     8 import org.apache.commons.collections.ComparatorUtils;
     9 import org.apache.commons.collections.comparators.ComparableComparator;
    10 import org.apache.commons.collections.comparators.ComparatorChain;
    11 
    12 public class ComplexComparator {
    13     private int  id;      
    14     private String  name;      
    15     private String age;      
    16     
    17     public ComplexComparator(){}
    18     
    19     public ComplexComparator(int id, String age,String name) {      
    20         this.id = id;      
    21         this.name = name;      
    22         this.age = age;      
    23     }  
    24     
    25     @SuppressWarnings({ "unchecked", "rawtypes" })  
    26     public static void main(String[] args) {      
    27       
    28         //在列表中加入若干ComplexComparator对象  
    29         ArrayList<Object> list = new ArrayList<Object>();      
    30         list.add(new ComplexComparator(1,"3","五"));      
    31         list.add(new ComplexComparator(1,"1","六"));      
    32         list.add(new ComplexComparator(1,"5","二"));      
    33         list.add(new ComplexComparator(1,"4","四"));      
    34         list.add(new ComplexComparator(4,"2","一"));      
    35   
    36         //创建一个排序规则  
    37         Comparator mycmp = ComparableComparator.getInstance();      
    38         mycmp = ComparatorUtils.nullLowComparator(mycmp);  //允许null         
    39         mycmp = ComparatorUtils.reversedComparator(mycmp); //逆序         
    40   
    41         //声明要排序的对象的属性,并指明所使用的排序规则,如果不指明,则用默认排序  
    42         ArrayList<Object> sortFields = new ArrayList<Object>();      
    43         sortFields.add(new BeanComparator("id", mycmp)); //id逆序  (主)      
    44         sortFields.add(new BeanComparator("age"));      //name正序 (副)   
    45   
    46         //创建一个排序链  
    47         ComparatorChain multiSort = new ComparatorChain(sortFields);   
    48   
    49         //开始真正的排序,按照先主,后副的规则  
    50         Collections.sort(list,multiSort);      
    51   
    52         for (int i =0;i<list.size();i++) {      
    53             System.out.println(list.get(i));    //输出      
    54         }      
    55         /** 
    56          * 输出结果如下: 
    57          * [id=4,name=2,age=一] 
    58          * [id=1,name=1,age=六] 
    59          * [id=1,name=3,age=五] 
    60          * [id=1,name=4,age=四] 
    61          * [id=1,name=5,age=二] 
    62          */  
    63     }      
    64       
    65     public int getId() {      
    66         return this.id;      
    67     }      
    68   
    69     public void setId(int id) {      
    70         this.id = id;      
    71     }      
    72   
    73     public String getName() {      
    74         return this.name;      
    75     }      
    76   
    77     public void setName(String name) {      
    78         this.name = name;      
    79     }      
    80   
    81     public String toString() {      
    82         return "[id=" + this.id + ",name=" + this.name +",age=" + this.age +"]";      
    83     }      
    84   
    85     public String getAge() {      
    86         return age;      
    87     }      
    88   
    89     public void setAge(String age) {      
    90         this.age = age;      
    91     }      
    92 }

     按照某一项分组,然后按照另一项排序

     1 package com.sun.comparator;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Collections;
     5 import java.util.Comparator;
     6 import java.util.List;
     7 
     8 import org.apache.commons.beanutils.BeanComparator;
     9 import org.apache.commons.collections.ComparatorUtils;
    10 
    11 public class ComparatorUtilsDemo implements Comparator<ComparatorUtilsDemo> {
    12     private String name;
    13     private Integer age;
    14     private float score;
    15     public ComparatorUtilsDemo(){}
    16     
    17     public ComparatorUtilsDemo(String name, Integer age, float score) {
    18         super();
    19         this.name = name;
    20         this.age = age;
    21         this.score = score;
    22     }
    23     
    24     @Override
    25     public int compare(ComparatorUtilsDemo o1, ComparatorUtilsDemo o2) {
    26         if (o1.getScore() > o2.getScore())
    27             return -1;// 由高到底排序
    28         else if (o1.getScore() < o2.getScore())
    29             return 1;
    30         else
    31             return 0;
    32     }
    33     
    34     @Override
    35     public String toString() {
    36         return "name:" + name + ";age:" + age + ";score:" + score;
    37     }
    38     
    39     public static void main(String[] args) {
    40         List<ComparatorUtilsDemo> lists = new ArrayList<>();
    41         lists.add(new ComparatorUtilsDemo("a", 4, 4));
    42         lists.add(new ComparatorUtilsDemo("d", 4, 4));
    43         lists.add(new ComparatorUtilsDemo("a", 4, 4));
    44         lists.add(new ComparatorUtilsDemo("c", 4, 4));
    45         lists.add(new ComparatorUtilsDemo("a", 4, 4));
    46         lists.add(new ComparatorUtilsDemo("b", 4, 4));
    47         lists.add(new ComparatorUtilsDemo("b", 3, 6));
    48         lists.add(new ComparatorUtilsDemo("c", 2, 3));
    49         lists.add(new ComparatorUtilsDemo("d", 1, 7));
    50         lists.add(new ComparatorUtilsDemo("a", 1, 7));
    51         Collections.sort(lists, ComparatorUtils.chainedComparator(new BeanComparator("name"), new ComparatorUtilsDemo()));
    52         for (ComparatorUtilsDemo c : lists) {
    53             System.out.println("++++++" + c);
    54         }
    55     }
    56     
    57     public String getName() {
    58         return name;
    59     }
    60 
    61     public void setName(String name) {
    62         this.name = name;
    63     }
    64 
    65     public Integer getAge() {
    66         return age;
    67     }
    68 
    69     public void setAge(Integer age) {
    70         this.age = age;
    71     }
    72 
    73     public float getScore() {
    74         return score;
    75     }
    76 
    77     public void setScore(float score) {
    78         this.score = score;
    79     }
    80 }
  • 相关阅读:
    WinForm 无边框窗体 拖动工作区移动窗体
    [CSS]火狐和IE对css样式解释的差异
    md类型文件迁移至Notion(img资源也可以上传)
    google推出notebook软件
    刚收到几个google analytics 邀请,有真正需要的我可以送给他一个
    最近用drupal做了一个CMS网站
    google adsense 又增加了Picasa的推介,我已加上
    googlepages空间的一个bug
    近段时间比较郁闷
    开复与学生面对面
  • 原文地址:https://www.cnblogs.com/sun-space/p/5599153.html
Copyright © 2011-2022 走看看