comparable接口和comparator接口的应用与异同
public class Test {
public static void sort(){
List<String> st = new ArrayList<String>();
st.add("3");
st.add("2");
System.out.println(st);
Collections.sort(st);
System.out.println(st);
}
public static void sortInt(){
int[] ints ={1,3,2};
for(int i:ints)
System.out.println(i);
Arrays.sort(ints);
for(int i:ints)
System.out.println(i);
}
public static void sortSource(){
List<Source> soulist = new ArrayList<Source>();
Source a =new Source("a","1");
Source b =new Source("b","9");
Source c =new Source("c","3");
soulist.add(a);
soulist.add(b);
soulist.add(c);
for(Source sou:soulist)
System.out.println(sou.getAge());
Collections.sort(soulist);
for(Source sou:soulist)
System.out.println(sou.getAge());
}
public static void main(String[] args) throws Exception {
sort();
sortSource();
sortInt();
}
}
实体类source源码
public class Source implements Comparable {
private String name;
public Source(String name, String age) {
super();
this.name = name;
this.age = age;
}
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public int compareTo(Object o){
Source source = (Source)o;
String otherAge = source.getAge();
return this.age.compareTo(otherAge);
}
}
从中可以知道,对于Arrays和Collections这两个工具类中的sort方法在使用时,要排序的对象必须实现comparable接口并实现方法public int compareTo(Object o)
而对于String对象是由系统系统,其已经实现了comparable接口
而对于int等基本数据类型,不需要实现comparable接口
Collections.sort(T t) 这里的T需要实现comparable接口,默认使用comparable来实现排序
Collections.sort(T t,comparator) 使用comparator来实现
接下来介绍comparator接口的用法
一、实体类Step
package com.ljq.entity;
/**
* 运号单流程
*
* @author Administrator
*
*/
public class Step{
/** 处理时间 */
private String acceptTime = "";
/** 快件所在地点 */
private String acceptAddress = "";
public Step() {
super();
}
public Step(String acceptTime, String acceptAddress) {
super();
this.acceptTime = acceptTime;
this.acceptAddress = acceptAddress;
}
public String getAcceptTime() {
return acceptTime;
}
public void setAcceptTime(String acceptTime) {
this.acceptTime = acceptTime;
}
public String getAcceptAddress() {
return acceptAddress;
}
public void setAcceptAddress(String acceptAddress) {
this.acceptAddress = acceptAddress;
}
}
二、实现Comparator接口
package com.ljq.entity;
import java.util.Comparator;
import java.util.Date;
import com.ljq.util.UtilTool;
/**
* 对Step类进行排序
*
* @author Administrator
*
*/
public class StepComparator implements Comparator<Step>{
/**
* 如果o1小于o2,返回一个负数;如果o1大于o2,返回一个正数;如果他们相等,则返回0;
*/
@Override
public int compare(Step o1, Step o2) {
Date acceptTime1=UtilTool.strToDate(o1.getAcceptTime(), null);
Date acceptTime2=UtilTool.strToDate(o2.getAcceptTime(), null);
//对日期字段进行升序,如果欲降序可采用before方法
if(acceptTime1.after(acceptTime2)) return 1;
return -1;
}
}
三、测试
package junit;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
public class StepComparatorTest {
@Test
public void sort() throws Exception{
List<Step> steps=new ArrayList<Step>;
//对集合对象进行排序
StepComparator comparator=new StepComparator();
Collections.sort(steps, comparator);
if(steps!=null&&steps.size()>0){
for(Step step:steps){
System.out.println(step.getAcceptAddress());
System.out.println(step.getAcceptTime());
}
}
}
}
可见,在使用sort(T)方法时,则需要排序的实体类实现comparable接口
而在使用sort(T,comparator)方法时,则需要自定义一个其他类(非实体类)来实现comparator接口
两个接口,一个在实体类内部使用,一个在外部使用