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

来源: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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值