//这里简单介绍一下Java的Comparable内部比较器和Comparator外部比较器的用法实现
//那么我们来做一个关于对象间的排序,首先建一个Model或者叫JavaBen。如下:
//1.Java的Comparable内部比较器的用法实现:
//Comparable内部比较器(要让实体类JavaBen(TestComparable )实现Comparable接口并重写compareTo(Object o)方法)
//代码实现如下:
//public interface Comparable<T>此接口强行对实现它的每个类的对象进行整体排序。
//这种排序被称为类的自然排序,类的 compareTo 方法被称为它的自然比较方法。
package com.xiawei.test;
public class TestComparable implements Comparable<Object> {
/**
* 账单序号
*/
private int sequence = 0;
/**
* 账单名称
*/
private String billName = "";
public TestComparable(int sequence,String billName){
this.sequence = sequence;
this.billName = billName;
}
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
public String getBillName() {
return billName;
}
public void setBillName(String billName) {
this.billName = billName;
}
//重写Comparable接口的compareTo()方法//int compareTo(T
//参数:o - 要比较的对象。
//返回:负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。
@Override
public int compareTo(Object obj) {
// TODO Auto-generated method stub
return this.getSequence() - ((TestComparable) obj).getSequence();
} @Override
public String toString() {
return "TestComparable [账单序号=" + sequence + ", 账单名称=" + billName + "]";
}}
=======================================================================================
//测试一下效果,这里是根据账单对象的账单序号private int sequence 来比较排序的:
package com.xiawei;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;import com.xiawei.test.TestComparable;
public class Main {
public static void main(String[] args) {
//Comparable内部比较器(要让实体类(Model)实现Comparable接口并重写compareTo(Object o)方法)
List<TestComparable> lis = new ArrayList<TestComparable>();
lis.add(new TestComparable(1,"生活费支出"));
lis.add(new TestComparable(2,"旅游支出"));
Collections.sort(lis);
System.out.println(lis);
}}
输出结果:
[TestComparable [账单序号=1, 账单名称=生活费支出],
TestComparable [账单序号=2, 账单名称=旅游支出]]
=======================================================================================
//同样这里我们来做一个关于对象间的排序,所以首先建一个Model或者叫JavaBen。如下:
//2.Java的Comparator外部比较器的用法实现:
//Comparator外部比较器 (单独定义一个类实现Comparator接口并重写compare(Object o1,Object o2)方法)
package com.xiawei.model;
public class BillInfo {
/**
* 账单序号
*/
private int sequence = 0;
/**
* 账单名称
*/
private String billName = "";
/**
* 账单金额
*/
private double billMoney = 0;
/**
* 出账日期
*/
private String date = "";
public BillInfo(int sequence,String billName,double billMoney,String date){
this.sequence = sequence;
this.billName = billName;
this.billMoney = billMoney;
this.date = date;
}
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
public String getBillName() {
return billName;
}
public void setBillName(String billName) {
this.billName = billName;
}
public double getBillMoney() {
return billMoney;
}
public void setBillMoney(double billMoney) {
this.billMoney = billMoney;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "BillInfo [账单序号=" + sequence + ", 账单名称=" + billName + ", 账单金额=" + billMoney + ", 出账日期="
+ date + "]";
}
}
=====================================================================================
//单独定义一个类实现Comparator接口并重写compare(Object o1,Object o2)方法,代码如下:
// int compare(T o1,T
// 参数:o1 - 要比较的第一个对象。o2 - 要比较的第二个对象。
//返回:根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数。
package com.xiawei.summary;
import java.util.Comparator;
import com.xiawei.model.BillInfo;
public class BillInfoComparator implements Comparator<BillInfo> {
@Override
public int compare(BillInfo b1, BillInfo b2) {
BillInfo bill1 = (BillInfo) b1;
BillInfo bill2 = (BillInfo) b2;
int result = bill1.getSequence() > bill2.getSequence()?1:
(bill1.getSequence() == bill2.getSequence())?0:-1;
return result = result == 0?(bill1.getBillMoney() > bill2.getBillMoney()?1:-1):result;
}
}
=========================================================================
//测试一下效果,这里也是根据账单对象的账单序号private int sequence 来比较排序的:
package com.xiawei;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;import com.xiawei.model.BillInfo;
import com.xiawei.summary.BillInfoComparator;public class Main {
public static void main(String[] args) {
//Comparator外部比较器 (单独定义一个类实现Comparator接口并重写compare(Object o1,Object o2)方法)
List<BillInfo> list = new ArrayList<BillInfo>();
list.add(new BillInfo(4,"食品支出",100.0,"2017-01-01"));
list.add(new BillInfo(2,"旅游支出",200.0,"2017-02-01"));
list.add(new BillInfo(5,"教育支出",300.0,"2017-03-01"));
list.add(new BillInfo(3,"投资支出",400.0,"2017-04-01"));
list.add(new BillInfo(1,"交通支出",500.0,"2017-05-01"));
Collections.sort(list,new BillInfoComparator());
System.out.println(list);
}}
输出结果:
[BillInfo [账单序号=1, 账单名称=交通支出, 账单金额=500.0, 出账日期=2017-05-01],
BillInfo [账单序号=2, 账单名称=旅游支出, 账单金额=200.0, 出账日期=2017-02-01],
BillInfo [账单序号=3, 账单名称=投资支出, 账单金额=400.0, 出账日期=2017-04-01],
BillInfo [账单序号=4, 账单名称=食品支出, 账单金额=100.0, 出账日期=2017-01-01],
BillInfo [账单序号=5, 账单名称=教育支出, 账单金额=300.0, 出账日期=2017-03-01]]