使用Collections对list的内容进行排序
2017-10-31 11:26
453 查看
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<B> list = new ArrayList<B>();
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<B>(){
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<B> list) {
Iterator it = list.iterator(); // 得到迭代器,用于遍历list中的所有元素
while (it.hasNext()) {// 如果迭代器中有元素,则返回true
System.out.println("\t" + it.next().toString());// 显示该元素
}
}
}
class B implements Comparable<B>{
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()));
}
}
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<B> list = new ArrayList<B>();
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<B>(){
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<B> list) {
Iterator it = list.iterator(); // 得到迭代器,用于遍历list中的所有元素
while (it.hasNext()) {// 如果迭代器中有元素,则返回true
System.out.println("\t" + it.next().toString());// 显示该元素
}
}
}
class B implements Comparable<B>{
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()));
}
}
相关文章推荐
- 使用Collections.sort()方法扩展list集合中的数据并对其进行排序
- 使用Collections.sort对List进行排序
- 使用Collections.sort()对List/ArrayList进行排序 推荐
- 使用Collections.sort()对List/ArrayList进行排序
- 使用Collections对list进行排序
- Java中使用Collections.sort()方法对数字和字符串泛型的LIst进行排序
- Java-Collections的sort方法对list进行排序
- 不使用遍历循环、Linq与Lamba表达式快速对List集合进行筛选与排序——在集合类里使用Find、FindAll与Sort
- 使用c++的 list.sort()进行排序
- Java中使用Collections.sort对List里面的元素排序示例
- 使用Collections.sort()实现List正反排序的方法
- C# 中使用Linq和Lambda表达式对List<T>进行排序
- Collections.sort()对list进行排序
- 关于java中的List集合的排序问题/Collections.sort()的使用(转)
- 使用org.apache.commons.collections.CollectionUtils进行List与List比较
- java根据时间排序list,使用collections.sort()
- [转]C# 中使用委托对List<>进行排序和筛选
- J2SE基础夯实系列之List中的数据如何根据对象的某一个或多个字段排序引出Comparable和comparator的使用-- Collections与Arrays
- 处理Commadn事件--对BulletedList控件内容进行排序
- 使用Collections.sort方法对list排序的两种方法