您的位置:首页 > 编程语言 > Java开发

自定义数据排序(Comparable,Comparator)JAVA126

2015-11-23 20:00 309 查看
来源:http://www.bjsxt.com/

1、S02E126_01自定义数据排序

两种方式:

(1)实体类:java.lang.Comparable + compareTo

(2)业务排序类:java.util.Comparator + compare

推荐第二种的原因:

——解耦:与实体类分离

——方便:应对多变的排序规则

第一种:

package com.test.custom.sort;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* 新闻条目实体类
* <br>排序:时间降序+点击量升序+标题降序
*/
public class NewsItem implements Comparable<NewsItem>{

private String title;//标题
private int hits;//点击量
private Date pubTime;//时间
public NewsItem() {

}
public NewsItem(String title, int hits, Date pubTime) {
super();
this.title = title;
this.hits = hits;
this.pubTime = pubTime;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getHits() {
return hits;
}

public void setHits(int hits) {
this.hits = hits;
}

public Date getPubTime() {
return pubTime;
}

public void setPubTime(Date pubTime) {
this.pubTime = pubTime;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("标题:").append(title);
sb.append(",时间:").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(pubTime));
sb.append(",点击量:").append(hits).append("\n");
return sb.toString();
}
/**
* 排序:时间降序+点击量升序+标题降序
*/
public int compareTo(NewsItem o) {
int result = 0;
result = -this.pubTime.compareTo(o.pubTime);//时间降序
if(0 == result){//时间相同
result = this.hits - o.hits;//点击量升序
if(0 == result){//点击量相同
result = -this.title.compareTo(o.title);//标题降序
}
}
return result;
}

public static void main(String[] args) {
List<NewsItem> news = new ArrayList<NewsItem>();
news.add(new NewsItem("中国第三季度物价水平大幅度上涨", 200, new Date(System.currentTimeMillis() - 1000*60*60)));
news.add(new NewsItem("猪肉涨价了", 100, new Date()));
news.add(new NewsItem("人民币贬值了,对人民生活会产生什么影响?", 50, new Date(System.currentTimeMillis() - 1000*60*60)));
System.out.println("排序前:" + news);
Collections.sort(news);
System.out.println("排序后:" + news);
}
}


第二种:

package com.test.custom.sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* 商品按价格的业务类(降序)排序,按收藏量的业务类(升序)排序
*/
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;
}

@Override
public String toString() {
return "商品名:" + name + ",收藏量:" + fav + ",价格:" + price + "\n";
}

public static void main(String[] args) {
List<Goods> list = new ArrayList<Goods>();
list.add(new Goods("牛奶",100,7000));
list.add(new Goods("进口牛奶",300,5000));
list.add(new Goods("免税牛奶",200,6000));
list.add(new Goods("本地牛奶",200,4000));
System.out.println("排序前:" + list.toString());

Collections.sort(list, new PriceComp());
System.out.println("按价格降序后:" + list.toString());

Collections.sort(list, new FavComp());
System.out.println("按收藏量升序后:" + list.toString());
}
}
/**
* 按价格排序的业务类(降序)
*/
class PriceComp implements Comparator<Goods>{

@Override
public int compare(Goods o1, Goods o2) {
return -(o1.getPrice()-o2.getPrice()>0?1:(o1.getPrice()==o2.getPrice()?0:-1));
}
}
/**
* 按收藏量排序的业务类(升序)
*/
class FavComp implements Comparator<Goods>{

@Override
public int compare(Goods o1, Goods o2) {
return o1.getFav() - o2.getFav();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: