zoukankan      html  css  js  c++  java
  • java学习(十六):对象的自定义比较,Comparator和Comparable

    通过两个例子实现对象的自定义排序

    1、实现Comparator接口。

     1 import java.util.ArrayList;
     2 import java.util.Collections;
     3 import java.util.Comparator;
     4 import java.util.List;
     5 
     6 public class StudentComparator implements Comparator<Object>
     7 {
     8     @Override
     9     public int compare(Object arg0, Object arg1)
    10     {
    11         return ((Student)arg0).getId() - ((Student)arg1).getId();
    12     }
    13     
    14     public static void main(String[] args)
    15     {
    16         Student stu1 = new Student(11, "A");
    17         Student stu2 = new Student(33, "B");
    18         Student stu3 = new Student(22, "C");
    19         Student stu4 = new Student(55, "D");
    20         List<Student> stuList = new ArrayList<Student>(4);
    21         stuList.add(stu1);
    22         stuList.add(stu2);
    23         stuList.add(stu3);
    24         stuList.add(stu4);
    25         Collections.sort(stuList, new StudentComparator());
    26         System.out.println(stuList);
    27     }
    28 }
    29 
    30 class Student
    31 {
    32     private int id;
    33     
    34     private String name;
    35     
    36     public Student(int id, String name)
    37     {
    38         this.id = id;
    39         this.name = name;
    40     }
    41     
    42     public int getId()
    43     {
    44         return id;
    45     }
    46     
    47     public void setId(int id)
    48     {
    49         this.id = id;
    50     }
    51     
    52     public String getName()
    53     {
    54         return name;
    55     }
    56     
    57     public void setName(String name)
    58     {
    59         this.name = name;
    60     }
    61     
    62     public String toString()
    63     {
    64         return this.getId() + this.getName();
    65     }
    66 }

    输出:[11A, 22C, 33B, 55D]

    2、在需要排序的类上直接实现Comparable接口

     1 import java.util.ArrayList;
     2 import java.util.Collections;
     3 import java.util.List;
     4 
     5 public class Student implements Comparable<Object>
     6 {
     7     private int id;
     8     
     9     private String name;
    10     
    11     public Student(int id, String name)
    12     {
    13         this.id = id;
    14         this.name = name;
    15     }
    16     
    17     public int getId()
    18     {
    19         return id;
    20     }
    21     
    22     public void setId(int id)
    23     {
    24         this.id = id;
    25     }
    26     
    27     public String getName()
    28     {
    29         return name;
    30     }
    31     
    32     public void setName(String name)
    33     {
    34         this.name = name;
    35     }
    36     
    37     public String toString()
    38     {
    39         return this.getId() + this.getName();
    40     }
    41     
    42     @Override
    43     public int compareTo(Object o)
    44     {
    45         return this.getId() - ((Student)o).getId();
    46     }
    47     
    48     public static void main(String[] args)
    49     {
    50         Student stu1 = new Student(11, "A");
    51         Student stu2 = new Student(33, "B");
    52         Student stu3 = new Student(22, "C");
    53         Student stu4 = new Student(55, "D");
    54         List<Student> stuList = new ArrayList<Student>(4);
    55         stuList.add(stu1);
    56         stuList.add(stu2);
    57         stuList.add(stu3);
    58         stuList.add(stu4);
    59         Collections.sort(stuList);
    60         System.out.println(stuList);
    61     }
    62     
    63 }

    同样输出:[11A, 22C, 33B, 55D]

    3、一个类实现了Comparable接口则表明这个类自身对象之间是可以相互比较的,这个类对象组成的集合就可以直接使用sort方法排序。

      Comparator可以看成一种算法的实现,将算法和数据分离。Comparator也可以在下面两种环境下使用:

      (1)类的设计师没有考虑到比较问题而没有实现Comparable,可以通过Comparator来实现排序而不必改变对象本身。

      (2)可以使用多种排序标准,比如定义升序类、降序类等。

  • 相关阅读:
    ElasticSearch(ES)学习笔记
    Lucene學習日志
    velocity代码生成器的使用
    springboot学习笔记
    springmvc json 类型转换错误
    在做del业务时,传递参数,和接口中入参注释
    做add添加业务时,字符集乱码,form标签库,button的href 问题,添加后页面跳转,forward,redirect 。定制错误输出
    mybatis中联合查询的返回结果集
    mybatis分页,绝对路径的2种写法
    maven导入项目时报错,配置应用程序监听器[org.springframework.web.context.ContextLoaderListener]错误
  • 原文地址:https://www.cnblogs.com/moleme/p/4420237.html
Copyright © 2011-2022 走看看