zoukankan      html  css  js  c++  java
  • 比较器:Compare接口与Comparator接口区别与理解

    一、实现Compare接口与Comparator接口的类,都是为了对象实例数组排序的方便,因为可以直接调用

     java.util.Arrays.sort(对象数组名称),可以自定义排序规则。

    不同之处:

     1 排序规则实现的方法不同

        Comparable接口的方法:compareTo(Object o)

        Comparator接口的方法:compare(T o1, To2)

     2 类设计前后不同

          Comparable接口用于在类的设计中使用;

       Comparator接口用于类设计已经完成,还想排序(Arrays);

    二、Comparable接口的实例操作

     Student类创建时实现Comparable接口,覆写compareTo()方法,

    成绩按从高到低排序,成绩相等按年龄从小到大排序。

    package ch11.lei.ji;
    /*实现Comparator接口的类可以方便的排序,
     * 覆写compareTo接口
     * java.util.Arrays.sort(对象类数组),*/
    class Student implements Comparable<Student> {    // 指定类型为Student
        private String name ;
        private int age ;
        private float score ;
        public Student(String name,int age,float score){
            this.name = name ;
            this.age = age ;
            this.score = score ;
        }
        public String toString(){
            return name + "		" + this.age + "		" + this.score ;
        }
        public int compareTo(Student stu){    // 覆写compareTo()方法,实现排序规则的应用
            if(this.score>stu.score){
                return -1 ;
            }else if(this.score<stu.score){
                return 1 ;
            }else{
                if(this.age>stu.age){
                    return 1 ;
                }else if(this.age<stu.age){
                    return -1 ;
                }else{
                    return 0 ;
                }
            }    
        }
    };
    public class Comparable01{
        public static void main(String args[]){
            Student stu[] = {new Student("张三",20,90.0f),
                new Student("李四",22,90.0f),new Student("王五",20,99.0f),
                new Student("赵六",20,70.0f),new Student("孙七",22,100.0f)} ;
            java.util.Arrays.sort(stu) ;    // 进行排序操作
            for(int i=0;i<stu.length;i++){    // 循环输出数组中的内容
                System.out.println(stu[i]) ;
            }
        }
    };

    三、Comparator接口实例操作

    Student01类原先没有比较器,类完成后构建一个比较器StudentComparator类

    按年龄从大到小排序。

    package ch11.lei.ji;
    import java.util.* ;
     class Student01{    // 指定类型为Student
        private String name ;
        private int age ;
        public Student01(String name,int age){
            this.name = name ;
            this.age = age ;
        }
        public boolean equals(Object obj){    // 覆写equals方法
            if(this==obj){
                return true ;
            }
            if(!(obj instanceof Student)){
                return false ;
            }
            Student01 stu = (Student01) obj ;
            if(stu.name.equals(this.name)&&stu.age==this.age){
                return true ;
            }else{
                return false ;
            }
        }
        public void setName(String name){
            this.name = name ;
        }
        public void setAge(int age){
            this.age = age ;
        }
        public String getName(){
            return this.name ;
        }
        public int getAge(){
            return this.age ;
        }
        public String toString(){
            return name + "		" + this.age  ;
        }
    };
    
    class StudentComparator implements Comparator<Student01>{    // 实现比较器
        // 因为Object类中本身已经有了equals()方法
        public int compare(Student01 s1,Student01 s2){
            if(s1.equals(s2)){
                return 0 ;
            }else if(s1.getAge()<s2.getAge()){    // 按年龄比较
                return 1 ;
            }else{
                return -1 ;
            }
        }
    };
    
    public class Comparator01{
        public static void main(String args[]){
            Student01 stu[] = {new Student01("张三",20),
                new Student01("李四",22),new Student01("王五",20),
                new Student01("赵六",20),new Student01("孙七",22)} ;
            java.util.Arrays.sort(stu,new StudentComparator()) ;    // 进行排序操作
            for(int i=0;i<stu.length;i++){    // 循环输出数组中的内容
                System.out.println(stu[i]) ;
            }
        }
    };
  • 相关阅读:
    防删没什么意思啊,直接写废你~
    绝大多数情况下,没有解决不了的问题,只有因为平时缺少练习而惧怕问题的复杂度,畏惧的心理让我们选择避让,采取并不那么好的方案去解决问题
    Java 模拟面试题
    Crossthread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on
    一步步从数据库备份恢复SharePoint Portal Server 2003
    【转】理解 JavaScript 闭包
    Just For Fun
    The database schema is too old to perform this operation in this SharePoint cluster. Please upgrade the database and...
    Hello World!
    使用filter筛选刚体碰撞
  • 原文地址:https://www.cnblogs.com/duange/p/6135943.html
Copyright © 2011-2022 走看看