zoukankan      html  css  js  c++  java
  • Comparable与Comparator区别(实现和使用)

    一、Comparable接口

    1.Comparable接口是什么?

    •  此接口强行对实现它的每个类的对象进行整体排序。此排序被称为该类的自然排序 ,类的 compareTo 方法被称为它的自然比较方法 。
    •  实现此接口的对象列表(和数组)可以通过 Collections.sort (和Arrays.sort )进行自动排序。
    •  实现此接口的对象可以用作有序映射表中的键或有序集合中的元素,无需指定比较器。 

    2.实现什么方法?

    •  int compareTo(T o):比较此对象与指定对象的顺序。如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数。
    •  参数: o - 要比较的对象。 
    •  返回:负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。
    •  抛出: ClassCastException - 如果指定对象的类型不允许它与此对象进行比较。

    3.实例

    • 代码如下:
    class Student implements Comparable<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+"		"+age+"		"+score;
        }
        @Override
        public int compareTo(Student o) {
            // TODO Auto-generated method stub
            if(this.score>o.score)//score是private的,为什么能够直接调用,这是因为在Student类内部
                return -1;//由高到底排序
            else if(this.score<o.score)
                return 1;
            else{
                if(this.age>o.age)
                    return 1;//由底到高排序
                else if(this.age<o.age)
                    return -1;
                else
                    return 0;
            }
        }
    }
    public class ComparableDemo01 {
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Student stu[]={new Student("zhangsan",20,90.0f),
                    new Student("lisi",22,90.0f),
                    new Student("wangwu",20,99.0f),
                    new Student("sunliu",22,100.0f)};
            java.util.Arrays.sort(stu);
            for(Student s:stu)
            {
                System.out.println(s);
            }
        }
    }
    • 程序运行结果:

    sunliu 22 100.0
    wangwu 20 99.0
    zhangsan 20 90.0
    lisi 22 90.0

    二、Comparator接口

    1.Comparator接口是什么?

    • 在设计类的时候,往往没有考虑到让类实现Comparable接口,那么我们就需要用到另外的一个比较器接口Comparator。
    • Comparator位于包java.util下,而Comparable位于包java.lang下,Comparable接口将比较代码嵌入自身类中,而后者在一个独立的类中实现比较。
    • 从上面的实例我们可以发现,compareTo(T o)只有一个参数,而Comparator接口中必须要实现的compare(T o1,T o2)就有两个参数。

     2.实例 代码如下:

    package edu.sjtu.ist.comutil;
    import java.util.Comparator;
    class 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 getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public float getScore() {
            return score;
        }
        public void setScore(float score) {
            this.score = score;
        }
        public String toString()
        {
            return name+"		"+age+"		"+score;
        }
    }
    class StudentComparator implements Comparator<Student>{
        @Override
        public int compare(Student o1, Student o2) {
            // TODO Auto-generated method stub
            if(o1.getScore()>o2.getScore())
                return -1;
            else if(o1.getScore()<o2.getScore())
                return 1;
            else{
                if(o1.getAge()>o2.getAge())
                    return 1;
                else if(o1.getAge()<o2.getAge())
                    return -1;
                else 
                    return 0;
            }
        }
    
    }
    public class ComparableDemo02 {
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Student stu[]={new Student("zhangsan",20,90.0f),
                    new Student("lisi",22,90.0f),
                    new Student("wangwu",20,99.0f),
                    new Student("sunliu",22,100.0f)};
            java.util.Arrays.sort(stu,new StudentComparator());
            for(Student s:stu)
            {
                System.out.println(s);
            }
        }
    }
  • 相关阅读:
    浏览器 页面报错
    TP单字母函数
    TP系统常量、项目后台的搭建、模板中常量字符串替换
    ThinkPHP3.2的简单知识
    面向对象基本概念,关键字,类的组成,静态,三大特性,创建类和对象,设计模式
    layui(弹出层)
    头像文件上传 方法一:from表单 方法二:ajax
    上传文件
    流程增加,发起事件,流程显示,处理。
    分页,条件查找后再分页
  • 原文地址:https://www.cnblogs.com/zouzz/p/6088985.html
Copyright © 2011-2022 走看看