详解Java中Collections.sort排序
Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能;如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f,g这样,当然数字也是这样的。
compare(a,b)方法:根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数。
equals(obj)方法:仅当指定的对象也是一个Comparator,并且强行实施与此Comparator相同的排序时才返回true。
Collections.sort(list,newPriceComparator());的第二个参数返回一个int型的值,就相当于一个标志,告诉sort方法按什么顺序来对list进行排序。
具体实现代码方法如下:
Book实体类:
packagecom.tjcyjd.comparator;
importjava.text.DecimalFormat;
importjava.text.SimpleDateFormat;
importjava.util.GregorianCalendar;
importjava.util.Iterator;
importjava.util.TreeMap;
/**
*书实体类
*
*@authoryjd
*
*/
publicclassBookimplementsComparable{//定义名为Book的类,默认继承自Object类
publicintid;//编号
publicStringname;//名称
publicdoubleprice;//价格
privateStringauthor;//作者
publicGregorianCalendarcalendar;//出版日期
publicBook(){
this(0,"X",0.0,newGregorianCalendar(),"");
}
publicBook(intid,Stringname,doubleprice,GregorianCalendarcalender,
Stringauthor){
this.id=id;
this.name=name;
this.price=price;
this.calendar=calender;
this.author=author;
}
//重写继承自父类Object的方法,满足Book类信息描述的要求
publicStringtoString(){
StringshowStr=id+"\t"+name;//定义显示类信息的字符串
DecimalFormatformatPrice=newDecimalFormat("0.00");//格式化价格到小数点后两位
showStr+="\t"+formatPrice.format(price);//格式化价格
showStr+="\t"+author;
SimpleDateFormatformatDate=newSimpleDateFormat("yyyy年MM月dd日");
showStr+="\t"+formatDate.format(calendar.getTime());//格式化时间
returnshowStr;//返回类信息字符串
}
publicintcompareTo(Objectobj){//Comparable接口中的方法
Bookb=(Book)obj;
returnthis.id-b.id;//按书的id比较大小,用于默认排序
}
publicstaticvoidmain(String[]args){
Bookb1=newBook(10000,"红楼梦",150.86,newGregorianCalendar(2009,
01,25),"曹雪芹、高鄂");
Bookb2=newBook(10001,"三国演义",99.68,newGregorianCalendar(2008,7,
8),"罗贯中");
Bookb3=newBook(10002,"水浒传",100.8,newGregorianCalendar(2009,6,
28),"施耐庵");
Bookb4=newBook(10003,"西游记",120.8,newGregorianCalendar(2011,6,
8),"吴承恩");
Bookb5=newBook(10004,"天龙八部",10.4,newGregorianCalendar(2011,9,
23),"搜狐");
TreeMaptm=newTreeMap();
tm.put(b1,newInteger(255));
tm.put(b2,newInteger(122));
tm.put(b3,newInteger(688));
tm.put(b4,newInteger(453));
tm.put(b5,newInteger(40));
Iteratorit=tm.keySet().iterator();
Objectkey=null,value=null;
Bookbb=null;
while(it.hasNext()){
key=it.next();
bb=(Book)key;
value=tm.get(key);
System.out.println(bb.toString()+"\t库存:"+tm.get(key));
}
}
}
自定义比较器和测试类:
packagecom.tjcyjd.comparator;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.Comparator;
importjava.util.GregorianCalendar;
importjava.util.Iterator;
importjava.util.List;
publicclassUseComparator{
publicstaticvoidmain(Stringargs[]){
Listlist=newArrayList();//数组序列
Bookb1=newBook(10000,"红楼梦",150.86,newGregorianCalendar(2009,
01,25),"曹雪芹、高鄂");
Bookb2=newBook(10001,"三国演义",99.68,newGregorianCalendar(2008,7,
8),"罗贯中");
Bookb3=newBook(10002,"水浒传",100.8,newGregorianCalendar(2009,6,
28),"施耐庵");
Bookb4=newBook(10003,"西游记",120.8,newGregorianCalendar(2011,6,
8),"吴承恩");
Bookb5=newBook(10004,"天龙八部",10.4,newGregorianCalendar(2011,9,
23),"搜狐");
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
list.add(b5);
//Collections.sort(list);//没有默认比较器,不能排序
System.out.println("数组序列中的元素:");
myprint(list);
Collections.sort(list,newPriceComparator());//根据价格排序
System.out.println("按书的价格排序:");
myprint(list);
Collections.sort(list,newCalendarComparator());//根据时间排序
System.out.println("按书的出版时间排序:");
myprint(list);
}
//自定义方法:分行打印输出list中的元素
publicstaticvoidmyprint(Listlist){
Iteratorit=list.iterator();//得到迭代器,用于遍历list中的所有元素
while(it.hasNext()){//如果迭代器中有元素,则返回true
System.out.println("\t"+it.next());//显示该元素
}
}
//自定义比较器:按书的价格排序
staticclassPriceComparatorimplementsComparator{
publicintcompare(Objectobject1,Objectobject2){//实现接口中的方法
Bookp1=(Book)object1;//强制转换
Bookp2=(Book)object2;
returnnewDouble(p1.price).compareTo(newDouble(p2.price));
}
}
//自定义比较器:按书出版时间来排序
staticclassCalendarComparatorimplementsComparator{
publicintcompare(Objectobject1,Objectobject2){//实现接口中的方法
Bookp1=(Book)object1;//强制转换
Bookp2=(Book)object2;
returnp2.calendar.compareTo(p1.calendar);
}
}
}
以上所述是小编给大家介绍的详解Java中Collections.sort排序,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!