当前位置: 移动技术网 > IT编程>开发语言>Java > java中Collections.sort排序函数用法详解

java中Collections.sort排序函数用法详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

无限挑战111126,经济纠纷起诉流程,国投罗钾贴吧

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, new pricecomparator());的第二个参数返回一个int型的值,就相当于一个标志,告诉sort方法按什么顺序来对list进行排序。

具体实现代码方法如下:

book实体类:

package com.tjcyjd.comparator; 
 
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), "曹雪芹、高鄂"); 
    book b2 = new book(10001, "三国演义", 99.68, new gregoriancalendar(2008, 7, 
        8), "罗贯中 "); 
    book b3 = new book(10002, "水浒传", 100.8, new gregoriancalendar(2009, 6, 
        28), "施耐庵 "); 
    book b4 = new book(10003, "西游记", 120.8, new gregoriancalendar(2011, 6, 
        8), "吴承恩"); 
    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)); 
    } 
  } 
} 

自定义比较器和测试类:

package com.tjcyjd.comparator; 
 
import java.util.arraylist; 
import java.util.collections; 
import java.util.comparator; 
import java.util.gregoriancalendar; 
import java.util.iterator; 
import java.util.list; 
 
public class usecomparator { 
  public static void main(string args[]) { 
    list<book> list = new arraylist<book>(); // 数组序列 
    book b1 = new book(10000, "红楼梦", 150.86, new gregoriancalendar(2009, 
        01, 25), "曹雪芹、高鄂"); 
    book b2 = new book(10001, "三国演义", 99.68, new gregoriancalendar(2008, 7, 
        8), "罗贯中 "); 
    book b3 = new book(10002, "水浒传", 100.8, new gregoriancalendar(2009, 6, 
        28), "施耐庵 "); 
    book b4 = new book(10003, "西游记", 120.8, new gregoriancalendar(2011, 6, 
        8), "吴承恩"); 
    book b5 = new book(10004, "天龙八部", 10.4, new gregoriancalendar(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, new pricecomparator()); // 根据价格排序 
    system.out.println("按书的价格排序:"); 
    myprint(list); 
    collections.sort(list, new calendarcomparator()); // 根据时间排序 
    system.out.println("按书的出版时间排序:"); 
    myprint(list); 
  } 
 
  // 自定义方法:分行打印输出list中的元素 
  public static void myprint(list<book> list) { 
    iterator it = list.iterator(); // 得到迭代器,用于遍历list中的所有元素 
    while (it.hasnext()) {// 如果迭代器中有元素,则返回true 
      system.out.println("\t" + it.next());// 显示该元素 
    } 
  } 
 
  // 自定义比较器:按书的价格排序 
  static class pricecomparator implements comparator { 
    public int compare(object object1, object object2) {// 实现接口中的方法 
      book p1 = (book) object1; // 强制转换 
      book p2 = (book) object2; 
      return new double(p1.price).compareto(new double(p2.price)); 
    } 
  } 
 
  // 自定义比较器:按书出版时间来排序 
  static class calendarcomparator implements comparator { 
    public int compare(object object1, object object2) {// 实现接口中的方法 
      book p1 = (book) object1; // 强制转换 
      book p2 = (book) object2; 
      return p2.calendar.compareto(p1.calendar); 
    } 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网