首页 > 编程知识 正文

java对象排序(Comparable)详细实例(java list compareto_java中的compareto方法以及LIst列表排序的详细介绍【转】)

时间:2023-05-04 19:08:45 阅读:123889 作者:2779

/*测试类/

打包测试;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import Java.util.Gregorian calendar;

import java.util.Iterator;

import java.util.List;

公共类用户比较器{

publicstaticvoidmain (string args [ ] ) {

列表列表=new ArrayList (; //数组序列

bookB1=newbook(10000,《红楼梦》,150.86,newGregorianCalendar ) 2009,

01、25 )、' hhdmf、爽口老鼠');

bookB2=newbook(10001,'三国演义',99.68,newGregorianCalendar ) 2008,7,

8 )、' zrdxf ';

bookB3=newbook(10002,《水浒传》,100.8,newGregorianCalendar ) 2009,6,

28 )、(施耐南);

bookB4=newbook(10003,'西游记',120.8,newGregorianCalendar ) 2011,6,

8 )、' mdyx ';

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 (列表; //没有默认比较器,无法排序

System.out.println (数组序列的元素: );

我的打印(列表;

collections.sort (列表,new PriceComparator ) ); //按价格排序

System.out.println ('按书的价格排序: );

我的打印(列表;

collections.sort(list,new CalendarComparator (); //按时间排序

System.out.println ('按图书出版时间顺序排列: );

我的打印(列表;

}

//自定义方法:将list内的要素在分店打印输出

publicstaticvoidmyprint{

Iterator it=list.iterator (; 得到//迭代器,遍历列表中的所有元素

wile(it.Hasnext ) () /如果迭代器有元素,则返回true

system.out.println((t ) it.next ) ); //显示其要素

}

}

//自定义比较器:书的价格顺序

staticclasspricecomparatorimplementscomparator {

在公共int比较(对象1,对象2 )//接口中实现方法

bookP1=(book ) object1; //strong制切换

bookP2=(book ) object2;

返回新双精度(P2.price ).com Pareto (new double ) P2.price );

}

}

//自定义比较器:按图书出版时间排序

staticclasscalendarcomparatorimplementscomparator

在公共int比较(对象1,对象2 )//接口中实现方法

bookP1=(book ) object1; //strong制切换

bookP2=(book ) object2;

returnp2. calendar.com Pareto (P1.calendar );

}

}

}

结果如下。

数组中的元素:

10000红楼梦150.86hh

dmf、畅快的老鼠2009年02月25日

10001三国演义99.68zrdxf 2008年08月08日

10002水浒传100.80施耐庵 2009年07月28日

10003西游记120.80mdyx2011年07月08日

10004天龙八部10.40搜狐2011年10月23日

按书的价格排序:

10004天龙八部10.40搜狐2011年10月23日

10001三国演义99.68zrdxf 2008年08月08日

10002水浒传100.80施耐庵 2009年07月28日

10003西游记120.80mdyx2011年07月08日

10000红楼梦150.86hhdmf、畅快的老鼠2009年02月25日

按书的出版时间排序:

10004天龙八部10.40搜狐2011年10月23日

10003西游记120.80mdyx2011年07月08日

10002水浒传100.80施耐庵 2009年07月28日

10000红楼梦150.86hhdmf、畅快的老鼠2009年02月25日

10001三国演义99.68zrdxf 2008年08月08日

package test;

import java.text.DecimalFormat;

import java.text.SimpleDateFormat;

import java.util.GregorianCalendar;

import java.util.Iterator;

import java.util.TreeMap;

/**

* 书实体类

*

* @author yjd

*

*/

public class Book implements Comparable { // 定义名为Book的类,默认继承自Object类

public int id;// 编号

public String name;// 名称

public double price; // 价格

private String author;// 作者

public GregorianCalendar calendar;// 出版日期

public Book() {

this(0, "X", 0.0, new GregorianCalendar(), "");

}

public Book(int id, String name, double price, GregorianCalendar calender,

String author) {

this.id = id;

this.name = name;

this.price = price;

this.calendar = calender;

this.author = author;

}

// 重写继承自父类Object的方法,满足Book类信息描述的要求

public String toString() {

String showStr = id + "t" + name; // 定义显示类信息的字符串

DecimalFormat formatPrice = new DecimalFormat("0.00");// 格式化价格到小数点后两位

showStr += "t" + formatPrice.format(price);// 格式化价格

showStr += "t" + author;

SimpleDateFormat formatDate = new SimpleDateFormat("yyyy年MM月dd日");

showStr += "t" + formatDate.format(calendar.getTime()); // 格式化时间

return showStr; // 返回类信息字符串

}

public int compareTo(Object obj) {// Comparable接口中的方法

Book b = (Book) obj;

return this.id - b.id; // 按书的id比较大小,用于默认排序

}

public static void main(String[] args) {

Book b1 = new Book(10000, "红楼梦", 150.86, new GregorianCalendar(2009,

01, 25), "hhdmf、畅快的老鼠");

Book b2 = new Book(10001, "三国演义", 99.68, new GregorianCalendar(2008, 7,

8), "zrdxf ");

Book b3 = new Book(10002, "水浒传", 100.8, new GregorianCalendar(2009, 6,

28), "施耐庵 ");

Book b4 = new Book(10003, "西游记", 120.8, new GregorianCalendar(2011, 6,

8), "mdyx");

Book b5 = new Book(10004, "天龙八部", 10.4, new GregorianCalendar(2011, 9,

23), "搜狐");

TreeMap tm = new TreeMap();

tm.put(b1, new Integer(255));

tm.put(b2, new Integer(122));

tm.put(b3, new Integer(688));

tm.put(b4, new Integer(453));

tm.put(b5, new Integer(40));

Iterator it = tm.keySet().iterator();

Object key = null, value = null;

Book bb = null;

while (it.hasNext()) {

key = it.next();

bb = (Book) key;

value = tm.get(key);

System.out.println(bb.toString() + "t库存:" + tm.get(key));

}

}

}

book类测试结果:

10000   红楼梦   150.86   hhdmf、畅快的老鼠   2009年02月25日  库存:255

10001  三国演义  99.68  zrdxf   2008年08月08日    库存:122

10002  水浒传  100.80  施耐庵   2009年07月28日    库存:688

10003  西游记  120.80  mdyx  2011年07月08日    库存:453

10004  天龙八部  10.40  搜狐  2011年10月23日    库存:40

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。