下面通过实例来具体使用Comparator和Comparable
public class Goods {
private String name;
private double price;
private int fav;
public Goods() {
}
public Goods(String name, double price, int fav) {
super();
this.name = name;
this.price = price;
this.fav = fav;
}
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 int getFav() {
return fav;
}
public void setFav(int fav) {
this.fav = fav;
}
//这里需要重写Object中的toString方法,因为正常的toString方法如下:
//public String toString() {
// return getClass().getName() + "@" + Integer.toHexString(hashCode());
//}
@Override
public String toString() {
return this.name+"--"+this.price+"--"+this.fav;
}
}
import java.util.Comparator;
//因为对于商品的要求不一样,有些人希望按照价格来排列,有些人希望按照人气指数来排,所以
//使用Comparator的好处在于解耦,也就是同步工作,互不干扰,这里按照价格来排序,当然也
//也可以按照其他的来排序
public class Goodsprice implements Comparator<Goods>{
@Override
public int compare(Goods o1, Goods o2) {
//按照升序排序,前面加负号表示为降序,其实正常应该这么写:
// return -(o1.getPrice()-o2.getPrice()>0?1:(o1.getPrice()==o2.getPrice()?0:-1));
return (int) -(o1.getPrice()-o2.getPrice());
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GoodsAPP {
public static void main(String[] args) {
List<Goods> list = new ArrayList<>();
list.add(new Goods("苹果", 10, 6));
list.add(new Goods("梨子", 12, 8));
list.add(new Goods("桃子", 12, 7));
list.add(new Goods("栗子", 20, 3));
list.add(new Goods("香蕉", 18, 10));
Collections.sort(list,new Goodsprice());
System.out.println(list);
}
}
输出结果为:[栗子--20.0--3, 香蕉--18.0--10, 梨子--12.0--8, 桃子--12.0--7, 苹果--10.0--6]
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 对于新闻,按照顺序,时间降序,点击量升序,标题降序
* 使用Comparable的顺序就是已经订好的
* @author Administrator
*/
public class NewsItem implements Comparable<NewsItem>{
private String title;
private int hit;
private String time;
public NewsItem() {
}
public NewsItem(String title, int hit, String time) {
super();
this.title = title;
this.hit = hit;
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getHit() {
return hit;
}
public void setHit(int hit) {
this.hit = hit;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
@Override
public int compareTo(NewsItem o) {
int result = 0;
result = -this.time.compareTo(o.time);
if(result==0){
result = this.hit-o.hit; //因为是int类型,所以直接相减,不能使用compareTo方法
if(result==0){
result = this.title.compareTo(o.title);
}
}
return result;
}
@Override
public String toString() {
try {
return this.title+"--"+this.hit+"--"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time).toLocaleString()+"\n";
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class NewsApp {
public static void main(String[] args) {
List<NewsItem> list = new ArrayList<>();
list.add(new NewsItem("LPL夺冠了",1000,"2020-9-19 17:20:00"));
list.add(new NewsItem("LMS夺冠了",10,"2050-9-23 18:20:00"));
list.add(new NewsItem("LCK夺冠了",800,"2017-9-23 19:20:00"));
Collections.sort(list);
System.out.println(list);
}
}
输出结果:[LMS夺冠了--10--2050-9-23 18:20:00
, LPL夺冠了--1000--2020-9-19 17:20:00
, LCK夺冠了--800--2017-9-23 19:20:00
]