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

    Comparable接口

    Comparable接口实际上属于比较器的操作接口,定义如下:

    public interface Comparable<T>{ 

    //接口上使用了泛型

     int compareTo(T o); //定义compareTo方法,此方法完成排序

    }

    compareTo()方法的三种返回值类型:

    小于:-1

    等于:0

    大于:1

     1 package com.fwj.compare;
     2 
     3 public class Student implements Comparable<Student> {
     4 
     5     private String name;
     6     private int age;
     7     private int score;
     8     
     9     public Student(String name, int age, int score) {
    10         super();
    11         this.name = name;
    12         this.age = age;
    13         this.score = score;
    14     }
    15 
    16     @Override
    17     public String toString() {
    18         return "Student [name=" + name + ", age=" + age + ", score=" + score
    19                 + "]";
    20     }
    21 
    22     @Override
    23     public int compareTo(Student stu) {    //按成绩升序排序,如果成绩相同按年龄升序排序
    24 
    25         if(this.score>stu.score){
    26             return 1;
    27         }else if(this.score<stu.score){
    28             return -1;
    29         }else{
    30             if(this.age>stu.age){
    31                 return 1;
    32             }else if(this.age<stu.age){
    33                 return -1;
    34             }else{
    35                 return 0;
    36             }
    37         }
    38     }
    39 
    40 }
    41 
    42 
    43 package com.fwj.compare;
    44 
    45 import java.util.Arrays;
    46 
    47 public class Test {
    48 
    49     public static void main(String[] args) {
    50         
    51         Student stu[] = {new Student("fwj",23,90),
    52                 new Student("hpf",26,83),
    53                 new Student("wh",25,83),
    54                 new Student("lw",20,86)
    55         };
    56         System.out.println("=================排序前===================");
    57         print(stu);
    58         System.out.println("=================排序后===================");
    59         Arrays.sort(stu);
    60         print(stu);
    61     }
    62     public static void print(Student stu[]){
    63     
    64         for (Student student : stu) {
    65             System.out.println(student);
    66         }
    67     }
    68 
    69 }

    运行结果:

     1 =================排序前===================
     2 Student [name=fwj, age=23, score=90]
     3 Student [name=hpf, age=26, score=83]
     4 Student [name=wh, age=25, score=83]
     5 Student [name=lw, age=20, score=86]
     6 =================排序后===================
     7 Student [name=wh, age=25, score=83]
     8 Student [name=hpf, age=26, score=83]
     9 Student [name=lw, age=20, score=86]
    10 Student [name=fwj, age=23, score=90]

    Comparator接口

    如果一个类已经开发完成,则肯定不能修改,所以,此时为了让此类具有排序功能,可以使用comparator完成排序的操作。

     1 package com.fwj.compare;
     2 
     3 public class Student {
     4 
     5     private String name;
     6     private int age;
     7     private int score;
     8     
     9     public String getName() {
    10         return name;
    11     }
    12 
    13     public void setName(String name) {
    14         this.name = name;
    15     }
    16 
    17     public int getAge() {
    18         return age;
    19     }
    20 
    21     public void setAge(int age) {
    22         this.age = age;
    23     }
    24 
    25     public int getScore() {
    26         return score;
    27     }
    28 
    29     public void setScore(int score) {
    30         this.score = score;
    31     }
    32 
    33     public Student(String name, int age, int score) {
    34         super();
    35         this.name = name;
    36         this.age = age;
    37         this.score = score;
    38     }
    39 
    40     @Override
    41     public boolean equals(Object obj) {//复写equals,完成对象比较
    42 
    43         if(this==obj){
    44             return true;
    45         }
    46         if(!(obj instanceof Student)){
    47             return false;
    48         }
    49         Student stu = (Student)obj;
    50         if(this.name==stu.name&&this.age==stu.age&&this.score==stu.score){
    51             return true;
    52         }else{
    53             return false;
    54         }
    55     }
    56 
    57     @Override
    58     public String toString() {
    59         return "Student [name=" + name + ", age=" + age + ", score=" + score
    60                 + "]";
    61     }
    62 
    63 }
     1 package com.fwj.compare;
     2 
     3 import java.util.Comparator;
     4 
     5 public class StudentComparetor implements Comparator<Student> {
     6 
     7     @Override
     8     public int compare(Student stu1, Student stu2) {
     9         if(stu1.getScore()>stu2.getScore()){
    10             return -1;
    11         }else if(stu1.getScore()<stu2.getScore()){
    12             return 1;
    13         }else{
    14             if(stu1.getAge()>stu2.getAge()){
    15                 return -1;
    16             }else if(stu1.getAge()<stu2.getAge()){
    17                 return 1;
    18             }else{
    19                 return 0;
    20             }
    21         }
    22     }
    23     
    24     //貌似实体类中复写了equals方法,此类中就不需要再次复写,,,,?????
    25     @Override
    26     public boolean equals(Object obj) {
    27         // TODO Auto-generated method stub
    28         return this.equals(obj);
    29     }
    30     
    31 }
     1 package com.fwj.compare;
     2 
     3 import java.util.Arrays;
     4 
     5 public class Test {
     6 
     7     public static void main(String[] args) {
     8         
     9         Student stu[] = {new Student("fwj",23,90),
    10                 new Student("hpf",26,83),
    11                 new Student("wh",25,83),
    12                 new Student("lw",20,86)
    13         };
    14         System.out.println("=================排序前===================");
    15         print(stu);
    16         System.out.println("=================排序后===================");
    17         Arrays.sort(stu, new StudentComparetor());
    18         print(stu);
    19     }
    20     public static void print(Student stu[]){
    21     
    22         for (Student student : stu) {
    23             System.out.println(student);
    24         }
    25     }
    26 
    27 }

     运行结果:

     1 =================排序前===================
     2 Student [name=fwj, age=23, score=90]
     3 Student [name=hpf, age=26, score=83]
     4 Student [name=wh, age=25, score=83]
     5 Student [name=lw, age=20, score=86]
     6 =================排序后===================
     7 Student [name=fwj, age=23, score=90]
     8 Student [name=lw, age=20, score=86]
     9 Student [name=hpf, age=26, score=83]
    10 Student [name=wh, age=25, score=83]
  • 相关阅读:
    点击图片跳转详情
    offsetwidth/clientwidth的区别
    css中让元素隐藏的多种方法
    js中的||、&&与!用法
    怎么区分静态网页和动态网页
    我的第一篇博客--新手勿喷
    2015腾讯暑期实习生面试
    ring0 与 ring3 层之间的交互
    驱动层得到进程的完整路径
    WinDbg调试流程的学习及对TP反调试的探索
  • 原文地址:https://www.cnblogs.com/mingluosunshan/p/3229973.html
Copyright © 2011-2022 走看看