容器的排序-3
来源:互联网 发布:2015全国各省旅游数据 编辑:程序博客网 时间:2024/06/07 03:39
下面通过实例来具体使用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 ]
阅读全文
0 0
- 容器的排序-3
- 对容器的排序
- java容器的排序
- set容器的排序
- java的容器排序
- STL 容器的排序
- 容器的排序-1
- 容器的排序-2
- Collections容器的排序方法
- 数组和容器的排序
- Set容器的排序规则
- map容器的排序问题
- 容器内对象的排序
- Java容器排序的方法
- C++容器排序算法的简单应用
- 对容器元素重新排序的算法
- 限定条件的容器数组排序输出
- 3、容器的使用
- javascript dom编程艺术学习笔记之动态创建标记
- 微信开放平台之公众号小程序第三方平台探讨总结
- 解决Cannot change version of project facet Dynamic web module to 2.5
- 关于优惠券优惠的思路以及实践
- mybatis与jdbc的比较
- 容器的排序-3
- Eclipse 引入js文件报错
- 离线安装Cloudera Manager 5和CDH5(最新版5.12.1) 完全教程
- 小结下spring mvc中的@RequestMapping的用法。
- Angular将填入表单的数据渲染到表格
- 在java中,接口定义为抽象的,有什么好处?
- java.lang.IllegalArgumentException: Request header is too large的解决
- linux htop命令
- Android Api demo系列(24) (Graphics>shadow card drag)