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
- Java-排序02
- java排序----鸡尾酒排序
- java排序--归并排序
- java排序-希尔排序
- Java 排序 - 冒泡排序
- Java排序 Collections排序
- Java排序--插入排序
- 排序-选择排序-Java
- 排序-插入排序-Java
- 排序-归并排序-Java
- 排序-快速排序-Java
- java排序 -- 堆排序
- JAVA排序
- JAVA排序
- java排序
- java排序
- java排序
- JAVA排序
- 堆栈的例子——来解决计算机的表达式计算
- 功率谱密度
- effective STL 读书笔记——第四章:迭代器
- EOJ 3260袋鼠妈妈找孩子 (DFS)
- Financial Management
- Java-排序02
- cxf整合spring 命名空间问题 (nested exception is org.xml.sax.SAXParseException)
- 计蒜客 跳跃游戏(dfs)
- java——缓冲流
- JS获取登录位置
- +-字符串
- pclzip压缩文件和解压的多种示例
- HDU 1698 Just a Hook(线段树 区间更新 + 查询区间和)
- shared_ptr直白点