您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Comparable与Comparator的比较与使用

2024/6/18 21:45:13发布25次查看
一 概述1.comparable与comparator使用背景数值型数据(byte int short long float double)天生可对比大小,可排序,string实现了comparable接口也可以对比大小与排序,而自定义类多种多样,没有一个共有的可以用作排序的指标,因此需要在自定义类中手动建立对比的方法,出于这个目的,java提供了两个接口comparable与comparator。
2.集合排序collections.sort()底层排序依靠的是arrays.sort(),而arrays.sort()排序时采用的是冒泡法。
二 comparable需要对比大小的对象可以实现comparable接口,实现其中的抽象方法,该抽象方法用来设定比较的方式。下面以一个示例进行说明:
1.实体类package com.javase.collections.comparable;public class student implements comparable<student> {private string name;private int score;public student() {super();     }public student(string name, int score) {super();this.name = name;this.score = score;     }public string getname() {return name;     }public void setname(string name) {this.name = name;     }public int getscore() {return score;     }public void setscore(int score) {this.score = score;     }     @overridepublic int compareto(student stu) {return this.score - stu.score;// 操作对象减去参数对象,升序排列,反之降序。    } }
在compareto()方法中,以属性score为排序指标,采用“this.score-stu.score”,最终结果以升序排列,反之降序。
2.测试类package com.javase.collections.comparable;import java.util.arraylist;import java.util.collections;import java.util.list;import org.junit.test;public class comparabletest {     @testpublic void testcomparable() {         list<student> stus = new arraylist<student>();         student zhangsan = new student(zhangsan, 100);         student lisi = new student(lisi, 90);         student wanger = new student(wanger, 95);         stus.add(zhangsan);         stus.add(lisi);         stus.add(wanger);         system.out.println(排序前);for (student x : stus) {             system.out.println(x.getname() + :: + x.getscore());         }         system.out.println(排序后);         collections.sort(stus);for (student x : stus) {             system.out.println(x.getname() + :: + x.getscore());         }     } }
输出:
三 comparator如果一个类在创建时未实现comparable接口,希望在不修改源码的情况下对其对象进行排序,可以在调用排序方法时实现comparator比较器接口,指定排序方法。下面以一个示例进行说明:
1.实体类package com.javase.collections.comparator;public class student {private string name;private int score;public student() {super();     }public student(string name, int score) {super();this.name = name;this.score = score;     }public string getname() {return name;     }public void setname(string name) {this.name = name;     }public int getscore() {return score;     }public void setscore(int score) {this.score = score;     } }
2.测试类package com.javase.collections.comparator;import java.util.arraylist;import java.util.collections;import java.util.comparator;import java.util.list;import org.junit.test;public class comparatortest {     @testpublic void test() {         list<student> stus = new arraylist<student>();         student zhangsan = new student(zhangsan, 100);         student lisi = new student(lisi, 90);         student wanger = new student(wanger, 95);         stus.add(zhangsan);         stus.add(lisi);         stus.add(wanger);         system.out.println(排序前);for (student x : stus) {             system.out.println(x.getname() + :: + x.getscore());         }         system.out.println(-----------------------);         collections.sort(stus, new comparator<student>() {             @overridepublic int compare(student stu01, student stu02) {// return stu01.getscore() - stu02.getscore();//升序return stu02.getscore() - stu01.getscore();// 降序            }         });         system.out.println(排序后);for (student x : stus) {             system.out.println(x.getname() + :: + x.getscore());         }     } }
在compare(student stu01, student stu02)方法中,以属性score为排序指标,采用“stu01.score-stu02.score”,最终结果升序排列,反之降序。
输出:
以上就是comparable与comparator的比较与使用的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product