Java-排序02

来源:互联网 发布:java interface 解释 编辑:程序博客网 时间:2024/05/29 04:34

Java-排序02(引用类型中的自定义类型)
与引用类型的排序一样
实现的方法也分两种
1.实例实现lang包下的Comparable接口,重写compare to方法
2.提供额外的业务规则排序类,实现utl下的Comparator接口,重写compare 方法,从而指定比较类

(1)测试一个例子,门户网站上新闻信息的排序
排序规则是先按时间降序,再按点击量升序,最后按标题降序
实现Comparable,重写compare to方法,来比较

public class NewsItem implements Comparable<NewsItem> {    Date time;    int hits;    String title;    public NewsItem(){    }    public NewsItem(Date time, int hits, String title) {        this.time = time;        this.hits = hits;        this.title = title;    }    @Override    public int compareTo(NewsItem newsItem) {        //排序规则是先按时间降序,再按点击量升序,最后按标题降序        int result = 0;        result = -this.time.compareTo(newsItem.time);//时间按降序排列        if (result == 0)//如果时间一样,按点击量升序        {            result = this.hits - newsItem.hits;            if (result == 0)//如果点击量一样,按标题降序            {                result = -this.title.compareTo(newsItem.title);            }        }        return result;    }    @Override    public String toString() {        StringBuilder stringBuilder = new StringBuilder();        stringBuilder.append("标题:" + title+ "\n");        stringBuilder.append("点击量:" + hits+ "\n");        stringBuilder.append("时间:" + time + "\n");        return stringBuilder.toString();    }}public class NewsItemApp {    public static void main(String[] args) {        List<NewsItem> news = new ArrayList<NewsItem>();        news.add(new NewsItem(new Date(System.currentTimeMillis()-1000*60*60),50,"习近平总书记对老挝进行国事访问"));        news.add(new NewsItem(new Date(),100,"特朗普赢得美国大选"));        news.add(new NewsItem(new Date(System.currentTimeMillis()-1000*60*60),200,"ISIS进行残酷的屠杀"));        System.out.println(news.toString());        System.out.println("============================");        Collections.sort(news);        System.out.println(news.toString());    }}
2.测试一个例子  例如网店中的按价格,收藏量,名字等进行排序。有可能之间并不关联,  此时可以用实现Comparator,重写compare to 方法
    public class Goods {        double price;        int fav;        String name;        public Goods(){}        public Goods(double price, int fav, String name) {            this.price = price;            this.fav = fav;            this.name = name;        }        @Override        public String toString() {            return "Goods{" +                    "price=" + price +                    ", fav=" + fav +                    ", name='" + name + '\'' +                    '}';        }    }    public class GoodsPriceCmp implements Comparator<Goods>{        @Override        public int compare(Goods o1, Goods o2) {           int result;           if (o1.price < o2.price)               result = 1;           else if (o1.price == o2.price)               result = 0;           else               result = -1;           return result;        }    }    public class GoodsApp {        public static void main(String[] args) {            List<Goods> goods = new ArrayList<>();            goods.add(new Goods(1000,20,"辣条"));            goods.add(new Goods(1500,35,"方便面"));            goods.add(new Goods(50,2,"火腿肠"));            System.out.println(goods.toString());            Collections.sort(goods,new GoodsPriceCmp());            System.out.println(goods.toString());    }}
总结:    一般来说,最好用提供额外的业务排序类,通过实现Comparator接口,重写compare方法来进行比较    因为1.解耦:与实体类无关,保持了实体类的封装       2.方便:能更好的应对多种不同的业务规则
0 0