java中的排序(自定义数据排序)--使用Collections的sort方法
排序:将一组数据按相应的规则 排列 顺序
- 内置引用类型(String,Integer..),内部已经指定规则,直接使用即可。----实现Comparable接口
1. 整数、 Integer..:根据基本数据类型大小
2. Character(字符):根据Unicode编码顺序
3. String(字符串):
1)如果其中一个是另一个起始开始的子串,返回长度之差,
2)否则返回第一个不相等的Unicode之差。
4. 日期:根据日期的长整型数比较。
- 自定义引用类型,需要按照业务规则排序。有两种方式,分别如下所述:
package top.wfaceboss.sort.refType2;
public class Goods {
// 价格
private double price;
// 商品名称
private String name;
// 收藏量
private int fav;
public Goods() {
}
public Goods(String name,double price, int fav) {
super();
this.price = price;
this.name = name;
this.fav = fav;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getFav() {
return fav;
}
public void setFav(int fav) {
this.fav = fav;
}
@Override
public String toString() {
return "商品名:" + this.name + ",收藏量:" + this.fav + ",价格:" + this.price + "\n";
}
}
(2)新建业务排序类(实现java.util.Comparator接口),编写符合业务要求的排序方法,如下是按照价格排序的业务类(降序)
package top.wfaceboss.sort.refType2; /**
* 按照价格排序的业务类(降序)
*
* @author Administrator
*
*/
public class GoodsPriceCompare implements java.util.Comparator<Goods> { @Override
public int compare(Goods o1, Goods o2) { return -(o1.getPrice()-o2.getPrice()>0?1:o1.getPrice()==o2.getPrice()?0:-1);//降序
} }
(3)使用业务排序类
package top.wfaceboss.sort.refType2; 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<Goods>();
list.add(new Goods("老马视频", 100, 2000));
list.add(new Goods("老高视频", 50, 2000));
list.add(new Goods("老裴视频", 1000, 1000));
System.out.println("排序前:" + list);
Collections.sort(list,new GoodsPriceCompare());
System.out.println("排序后:"+list);
}
}
package top.wfaceboss.sort.refType; import java.text.SimpleDateFormat;
import java.util.Date; /**
* 新闻条目实体类 排序方式: java.lang.Comparable+compareTo
*
* @author Administrator
* @param <T>
*
*/
public class NewsItem implements java.lang.Comparable<NewsItem> {
// 标题
private String title;
// 点击量
private int hits;
// 时间
private Date pubTime; public int getHits() {
return hits;
} public void setHits(int hits) {
this.hits = hits;
} 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 Date getPubTime() {
return pubTime;
} public void setPubTime(Date pubTime) {
this.pubTime = pubTime;
} // 时间降序+点击量升序+标题降序
@Override
public int compareTo(NewsItem o) {
System.out.println("============dd==========");
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;
} @Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("标题:").append(this.title);
sb.append(",时间:").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.pubTime));
sb.append(",点击量:").append(this.hits).append('\n');
return sb.toString();
} }
(2)使用java自带的Collections调用sort,对该实体类的实例进行排序:
package top.wfaceboss.sort.refType; import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List; /**
* 使用Collections
*
*/
public class NewsItemApp {
public static void main(String[] args) {
List<NewsItem> news = new ArrayList<NewsItem>();
news.add(new NewsItem("aaa", 50, new Date()));
news.add(new NewsItem("bbb", 60, new Date(System.currentTimeMillis() - 1000 * 60 * 60)));
news.add(new NewsItem("ccc", 30, new Date(System.currentTimeMillis() + 1000 * 60 * 60))); Collections.sort(news);
System.out.println(news);
}
}
java中的排序(自定义数据排序)--使用Collections的sort方法的更多相关文章
- Java中double类型的数据精确到小数点后两位
Java中double类型的数据精确到小数点后两位 多余位四舍五入,四种方法 一: double f = 111231.5585;BigDecimal b = new BigDecimal(f); d ...
- JSON(三)——java中对于JSON格式数据的解析之json-lib与jackson
java中对于JSON格式数据的操作,主要是json格式字符串与JavaBean之间的相互转换.java中能够解析JSON格式数据的框架有很多,比如json-lib,jackson,阿里巴巴的fast ...
- Java中final修饰的数据
目录 Java中final修饰的数据 有初始值的final域 final+基本数据类型 final+引用数据类型 final与static final 空白final域 final修饰的参数 基本数据 ...
- 怎么在java中创建一个自定义的collector
目录 简介 Collector介绍 自定义Collector 总结 怎么在java中创建一个自定义的collector 简介 在之前的java collectors文章里面,我们讲到了stream的c ...
- 转:Java中子类是否可以继承父类的static变量和方法而呈现多态特性
原文地址:Java中子类是否可以继承父类的static变量和方法而呈现多态特性 静态方法 通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法,关于static方法,声明 ...
- JAVA 中两种判断输入的是否是数字的方法__正则化_
JAVA 中两种判断输入的是否是数字的方法 package t0806; import java.io.*; import java.util.regex.*; public class zhengz ...
- java中多线程执行时,为何调用的是start()方法而不是run()方法
Thead类中start()方法和run()方法的区别 1,start()用来启动一个线程,当调用start()方法时,系统才会开启一个线程,通过Thead类中start()方法来启动的线程处于就绪状 ...
- Java中list集合自定义排序-2022新项目
一.业务场景 为了加快首页数据查询的效率,因此将首页查询的数据大多数都放在了缓存中,包括各种list集合数据.对这些 从缓存中获取的数据做了一个兜底处理,如果从缓存中没有获取到数据,则直接从数据库中去 ...
- javascript中对两个对象进行排序 和 java中的两个对象排序
javascript中的对象数组排序 一 定义一个对象数组 var text = [{"name":"张","age":24},{" ...
随机推荐
- Python Flask 构建微电影视频网站
前言 学完本教程,你将掌握: 1.学会使用整形.浮点型.路径型.字符串型正则表达式路由转化器 2.学会使用post与get请求.上传文件.cookie获取与相应.404处理 3.学会适应模板自动转义. ...
- Azure SQL 数据库仓库Data Warehouse (2) 架构
<Windows Azure Platform 系列文章目录> 在上一篇文章中,笔者介绍了MPP架构的基本内容 在本章中,笔者给大家介绍一下Azure SQL Data Warehouse ...
- Oracle 字符集更改
sqlplus sys/player as sysdba SQL*Plus: Release 11.2.0.1.0 Production shutdown immediate; startup mou ...
- modbus tcp 入门详解
Modbus tcp 格式说明 通讯机制 附C#测试工具用于学习,测试 前言: 之前的博客介绍了如何用C#来读写modbus tcp服务器的数据,文章:http://www.cnblogs.com ...
- JS一行代码,生成一个16进制随机颜色,简单粗暴。
var color = '#'+ Math.random().toString(16).substr(-6); document.body.style.backgroundColor = color; ...
- 【Graphite学习】系列学习文章-【转】
Graphite 系列 #2:Carbon 和 Whisper GRAPHITE SERIES #1: PROVISION HARDWARE GRAPHITE SERIES #2: CARBON &a ...
- asp.net利用HttpModule实现防sql注入和加载样式和JS文件
1.新建一个类,实现IHttpModule接口 代码如下: public class SqlHttpModule : IHttpModule { public void Dispose() { } p ...
- 【IIS错误】IIS各种错误
IIS简介 当用户试图通过HTTP或文件传输协议(FTP)访问一台正在运行Internet信息服务 (IIS)的服务器上的内容时,IIS返回一个表示该请求的状态的数字代码.该状态代码 记录在IIS日志 ...
- 阿里云安装kubernetes-UI报错endpoints \"kubernetes-dashboard\" not found解决方法
问题:阿里云ECS安装kube-ui v5后,访问 http://master_ip:8080/ui/跳转到http://master_ip:8080/api/v1/proxy/namespaces/ ...
- ZBench: 服务器一键测试脚本 / 自带结果导出vps网络测试
zbench主站: https://blog.liyuans.com/archives/Zbench.html 脚本来自友链 主机博客,项目地址为 https://github.com/Functio ...