您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
【第11章:Java常用类库】_比较器(Comparable、Comparator)
发布时间:2020-12-25 17:34:58编辑:雪饮阅读()
Comparable泛型接口
Comparable泛型接口用于实现对象数组的比较器,常用于对象数组的排序。
子类实现该接口的同时需要定义compareTo方法以覆写Comparable泛型接口中的compareTo方法,该方法返回有三种int型值,返回值为1表示大于,返回值为0表示相等,返回值为-1表示小于。
假如对学生的成绩和年龄进行排序,从小到大排序且先排序成绩,若成绩相同则排序年龄。
则实现如:
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 this.name+"\t"+this.age+"\t"+this.score;
}
public int compareTo(Student stu){
if(this.score>stu.score){
return 1;
}
if(this.score<stu.score){
return -1;
}
else{
if(this.age>stu.age){
return 1;
}
if(this.age<stu.age){
return -1;
}
return 0;
}
}
}
public class Hello{
public static void main(String args[]) throws Exception{
Student stu[]={
new Student("kasumi",24,96.5f),new Student("ayani",23,98.5f),new Student("hayabusa",28,98.5f),
new Student("snowDrink",18,90.1f),new Student("xy",18,92.2f),new Student("delightful",27,88.2f)
};
java.util.Arrays.sort(stu);
for(Student s:stu){
System.out.println(s);
}
}
};
D:\>javac Hello.java
D:\>java Hello
delightful 27 88.2
snowDrink 18 90.1
xy 18 92.2
kasumi 24 96.5
ayani 23 98.5
hayabusa 28 98.5
Comparable接口可以不用实现也能单独实例化
通过Integer为Comparable实例化
public class Hello{
public static void main(String args[]) throws Exception{
Comparable com=30;
System.out.println(com);
}
};
D:\>javac Hello.java
D:\>java Hello
30
直接输出的时候调用的是toString()方法
二叉树排序的实现
Comparable接口可以直接通过Integer实例化,则可以利用该特性实现二叉树排序
class BinaryTree{
class Node{
//Comparable实例化为Integer
private Comparable data;
private Node left;
private Node right;
public Node(Comparable data){
this.data = data ;
}
public void addNode(Node newNode){
//新节点比当前节点大,则新节点放到当前节点的右侧
if(newNode.data.compareTo(this.data)>=0){
if(this.right==null){
this.right=newNode;
}
else{
this.right.addNode(newNode);
}
}
else{
if(this.left==null){
this.left=newNode;
}
else{
this.left.addNode(newNode);
}
}
}
//中序遍历(先输出当前节点左侧 然后输出当前节点 最后输出当前节点右侧)
public void printNode(){
if(this.left!=null){
this.left.printNode();
}
System.out.println(this.data+"\t");
if(this.right!=null){
this.right.printNode();
}
}
}
private Node root;
public void add(Comparable data){
Node newNode=new Node(data);
if(this.root==null){
this.root=newNode;
}
else{
this.root.addNode(newNode);
}
}
public void print(){
this.root.printNode();
}
}
public class Hello{
public static void main(String args[]) throws Exception{
BinaryTree bt=new BinaryTree();
bt.add(2);
bt.add(2);
bt.add(0);
bt.add(8);
bt.add(0);
bt.add(7);
bt.print();
}
};
D:\>javac Hello.java
注意:Hello.java 使用了未经检查或不安全的操作。
注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。
D:\>java Hello
0
0
2
2
7
8
Comparator范例接口
Comparator范例接口与Comparable范例接口功能相同,都是可以通过实现比较器的形式来对对象数组排序。然区别是Comparator范例接口可以独立于业务对象所属类之外,对于业务对象所属类已经成形,不能被修改的情况下,则Comparator独立于业务对象所属类的特性则对于业务对象实例排序的调用者比较友好。
Comparator范例接口实现对象数组排序如:
import java.util.*;
class Student{
private String name;
private int age;
public Student(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){
return this.name+"\t"+this.age+"\t";
}
public boolean equals(Object obj){
if(this==obj){
return true;
}
if(!(obj instanceof Student)){
return false;
}
Student stu = (Student) obj ;
if(stu.equals(this.name) && this.age==stu.age){
return true;
}
else{
return false;
}
}
public int getAge(){
return this.age;
}
}
class StudentComparator implements Comparator<Student>{
public int compare(Student s1,Student s2){
if(s1.equals(s2)){
return 0 ;
}else if(s1.getAge()<s2.getAge()){
return 1 ;
}else{
return -1 ;
}
}
};
public class Hello{
public static void main(String args[]) throws Exception{
Student stu[]={
new Student("kasumi",24),new Student("ayani",23),new Student("hayabusa",28),
new Student("snowDrink",18),new Student("xy",18),new Student("delightful",27)
};
java.util.Arrays.sort(stu,new StudentComparator());
for(Student s:stu){
System.out.println(s);
}
}
};
D:\>javac Hello.java
D:\>java Hello
hayabusa 28
delightful 27
kasumi 24
ayani 23
snowDrink 18
xy 18
关键字词:java,Comparable,Comparator