java 接口类(排序)的简单实现
来源:互联网 发布:app相册制作软件 编辑:程序博客网 时间:2024/05/17 20:51
实体类:Java.lang.Comparable(接口) + comareTo(重写方法),业务排序类 java.util.Comparator(接口) + compare(重写方法).
一,在实际的需求中,我们需要根据对象的各种属性(标题,时间,点击率,销售额…)进行排序(升序,降序),可以在数据库的sql上进行处理,但是 不是每一个场景 都适合在sql上进行处理,我们有时候需要在程序根据不同的属性,对一个对象进行各种排序 通过页面呈现给用户。
下面有这样的一个需求,一种商品(商品名,销售量,生产日期),根据生产日期降序 销售量升序 商品名称降序
思路:首先按照日期降序,如果日期相同 按照销售量升序,如果销售量相同,按周商品的名称降序
1,创建需要比较的对象的Java bean
创建 Bean的快捷键:
1),带参数的构造器:// Shift + Alt + S –>O
2),不带参数的构造器: //Alt + / 生成空的构造方法
3),生成 get set方法:// Shift + Alt + S –> R + Table + Enter + Shift +Table –>Enter
/** * 商品po类 */public class Items implements java.lang.Comparable<Items> { private String title; private int hits; private Date pubTime; public Items() {} public Items(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 int compareTo(Items 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; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("商品名称").append(this.title); sb.append("销售量").append(this.hits); sb.append("生产时间").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.pubTime)).append("\n"); return sb.toString(); } }
2,造数据,比较
//时间降序, 销售量升序, 标题降序 public static void main(String[] args) { List<Items> item = new ArrayList<Items>(); item.add(new Items("abcitems" ,30,new Date(System.currentTimeMillis()-1000*60*60))); item.add(new Items("abcfgitems" ,30,new Date(System.currentTimeMillis()-1000*60*50))); item.add(new Items("abcditems" ,100,new Date())); item.add(new Items("abefNews" ,50,new Date(System.currentTimeMillis()-1000*60*60))); System.out.println("----------排序前----------"); System.out.println(item); System.out.println("----------排序后----------"); Collections.sort(item); System.out.println(item); }
二,Comparator的应用场景
一般比较字符串是按照unicode的大小进行排序的,但是我需要按照字符串的长度进行排序,下面是实现的案例:
首先,定义比较的业务规则
/** * 定义业务的比较规则,我需要按照字符串的长度进行比较(在实际的场景中,可以根据业务的需求,灵活的改变比较规则,实现排序) */public class CompareString implements java.util.Comparator<String> { @Override public int compare(String o1, String o2) { int len1 = o1.length(); int len2 = o2.length(); return -(len1-len2);//需要按照降序排列 }}
比较 字符串的长度,按照 降序排列
public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("abc"); list.add("abcd"); list.add("ab"); list.add("abd"); Collections.sort(list,new CompareString()); System.out.println(list); //[abcd, abc, abd, ab] }
比如 商品,我需要按照价格的降序排列,代码如下:
商品 po类
/** * 商品po类 */public class Products { private String title; private int price; public Products() {} public Products(String title, int price) { super(); this.title = title; this.price = price; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "title=" + title+",price=" + price +"\n"; }}
定义比较规则:
/** * 按照价格的降序排列 */ public class ProductCompare implements java.util.Comparator<Products> { @Override public int compare(Products o1, Products o2) { return -( o1.getPrice()-o2.getPrice()>0?1: (o1.getPrice()==o2.getPrice()?0:-1)); } }
数据比较:
public static void main(String[] args) { List<Products> product = new ArrayList<Products>(); product.add(new Products("a",120)); product.add(new Products("b",143432)); product.add(new Products("c",1892)); product.add(new Products("d",11092)); Collections.sort(product,new ProductCompare()); System.out.println(product); 结果: [title=b,price=143432 title=d,price=11092 title=c,price=1892 title=a,price=120 ] }
阅读全文
0 0
- java 接口类(排序)的简单实现
- Java 简单的接口实现
- Java自定义实现Comparable接口的类,实现多维排序
- Java实现简单的排序
- JAVA 多线程的简单实现(runnable接口方式)
- 一个简单的java web程序(servlet实现接口)
- Java基础之接口的简单实现
- Java实现的简单排序(冒泡排序,选择排序和插入排序)
- 简单排序java实现
- 数据结构(JAVA)---二叉树的简单实现及排序
- 快速排序的超简单实现(Java)
- 三种简单的排序算法(java实现)
- 一种简单的用java实现快速排序(Quicksort)
- 一种简单的用java实现快速排序(Quicksort)
- java快速排序算法的简单实现
- 归并排序 一个简单的java实现
- 简单的插入排序 java实现
- java实现简单的排序算法
- DrawerLayout的简单使用
- 微信小程序:支付系列合集(开发指南+精品Demo)
- Struts2的面试知识点
- Android应用商店——项目介绍
- 取得手机屏幕大小DisplayMetrics的应用
- java 接口类(排序)的简单实现
- iOS 一些常用的位运算
- 欢迎使用CSDN-markdown编辑器
- 自定义View实例——FlowLayout(流布局)的实现
- Unity移动障碍物设置
- 【已解决】Django传递数据到js提示Unexpected token
- 记,一次vivado_hls使用gcc编译错误的解决
- eclipse中代码push到github每次要输入github仓库地址
- SpringInAction学习笔记——第14章 保护方法应用