使用Collections對list的內容進行排序

petterchx發表於2021-09-09

import java.awt.print.Book;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class Col {

public static void main(String[] args) {
    List list = new ArrayList();
    B b1 = new B(1,"zengxiaohui",11.0,new Date());
    B b2 = new B(2,"zengxiaohu2",12.0,new Date());
    B b3 = new B(3,"zengxiaohu3",13.0,new Date());
    list.add(b3);
    list.add(b1);
    list.add(b2);
    System.out.println("陣列序列中的元素:");
    myprint(list);
    Collections.sort(list); // 根據價格排序
    System.out.println("按B中的list方式排序:");
    myprint(list);
    Collections.sort(list, new PriceComparator()); // 根據價格排序
    System.out.println("按書的價格排序:");
    myprint(list);
    Collections.sort(list, new CalendarComparator()); // 根據時間排序
    System.out.println("按書的出版時間排序:");
    myprint(list);

    Collections.sort(list,new Comparator(){  
        public int compare(B p1, B p2) {  
            return new Double(p1.getPrice()).compareTo(new Double(p2.getPrice()));  
        }  
    });  
    System.out.println("程式碼中的沒有累的排序:");
    myprint(list);
}

// 自定義比較器:按書的價格排序
static class PriceComparator implements Comparator {
    public int compare(Object object1, Object object2) {// 實現介面中的方法
        B p1 = (B) object1; // 強制轉換
        B p2 = (B) object2;
        return new Double(p1.getPrice()).compareTo(new Double(p2.getPrice()));
    }
}

// 自定義比較器:按書出版時間來排序
static class CalendarComparator implements Comparator {
    public int compare(Object object1, Object object2) {// 實現介面中的方法
        B p1 = (B) object1; // 強制轉換
        B p2 = (B) object2;
        return p2.getCalendar().compareTo(p1.getCalendar());
    }
}

// 自定義方法:分行列印輸出list中的元素
public static void myprint(List list) {
    Iterator it = list.iterator(); // 得到迭代器,用於遍歷list中的所有元素
    while (it.hasNext()) {// 如果迭代器中有元素,則返回true
        System.out.println("t" + it.next().toString());// 顯示該元素
    }
}

}
class B implements Comparable{
private int id;
private String name;
private Double price;
private Date calendar;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Double getPrice() {
    return price;
}

public void setPrice(Double price) {
    this.price = price;
}

public Date getCalendar() {
    return calendar;
}

public void setCalendar(Date calendar) {
    this.calendar = calendar;
}

public B(int id, String name, Double price, Date calendar) {
    super();
    this.id = id;
    this.name = name;
    this.price = price;
    this.calendar = calendar;
}

@Override
public String toString() {
    return "B [id=" + id + ", name=" + name + ", price=" + price + ", calendar=" + calendar + "]";
}

@Override
public int compareTo(B o) {
    return new Double(this.getPrice()).compareTo(new Double(o.getPrice()));
}

}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4606/viewspace-2799587/,如需轉載,請註明出處,否則將追究法律責任。

相關文章