【java】实现Comparable接口和Comparator接口,并重写compareTo方法和compare方法

实现Comparable接口和Comparator接口,并重写compareTo方法和compare方法

2016年08月11日 14:39:25 苏尹 阅读数:10772更多

个人分类: 学习笔记

实体类: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

 
  1. /**

  2. * 商品po类

  3. */

  4. public class Items implements java.lang.Comparable<Items> {

  5. private String title;

  6. private int hits;

  7. private Date pubTime;

  8. public Items() {}

  9. public Items(String title, int hits, Date pubTime) {

  10. super();

  11. this.title = title;

  12. this.hits = hits;

  13. this.pubTime = pubTime;

  14. }

  15. public String getTitle() {

  16. return title;

  17. }

  18. public void setTitle(String title) {

  19. this.title = title;

  20. }

  21. public int getHits() {

  22. return hits;

  23. }

  24. public void setHits(int hits) {

  25. this.hits = hits;

  26. }

  27. public Date getPubTime() {

  28. return pubTime;

  29. }

  30. public void setPubTime(Date pubTime) {

  31. this.pubTime = pubTime;

  32. }

  33. //时间降序 点击量升序 标题降序

  34. @Override

  35. public int compareTo(Items o) {

  36. int result = 0;

  37. //按照生产时间降序

  38. result = - this.pubTime.compareTo(o.pubTime);

  39. if(0==result){//如果生产时间相同 就按照销售量升序排列

  40. result = this.hits-o.hits;

  41. if(0==result){//如果销售量相同 按照名字降序排列

  42. result = - this.title.compareTo(o.title);

  43. }

  44. }

  45. return result;

  46. }

  47. @Override

  48. public String toString() {

  49. StringBuilder sb = new StringBuilder();

  50. sb.append("商品名称").append(this.title);

  51. sb.append("销售量").append(this.hits);

  52. sb.append("生产时间").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.pubTime)).append("\n");

  53. return sb.toString();

  54. }

  55. }

2,造数据,比较

 
  1. //时间降序, 销售量升序, 标题降序

  2. public static void main(String[] args) {

  3. List<Items> item

  4. = new ArrayList<Items>();

  5. item.add(new Items("abcitems"

  6. ,30,new Date(System.currentTimeMillis()-1000*60*60)));

  7. item.add(new Items("abcfgitems"

  8. ,30,new Date(System.currentTimeMillis()-1000*60*50)));

  9. item.add(new Items("abcditems"

  10. ,100,new Date()));

  11. item.add(new Items("abefNews"

  12. ,50,new Date(System.currentTimeMillis()-1000*60*60)));

  13. System.out.println("----------排序前----------");

  14. System.out.println(item);

  15. System.out.println("----------排序后----------");

  16. Collections.sort(item);

  17. System.out.println(item);

  18. }

二,Comparator的应用场景
一般比较字符串是按照unicode的大小进行排序的,但是我需要按照字符串的长度进行排序,下面是实现的案例:
首先,定义比较的业务规则

 
  1. /**

  2. * 定义业务的比较规则,我需要按照字符串的长度进行比较(在实际的场景中,可以根据业务的需求,灵活的改变比较规则,实现排序)

  3. */

  4. public class CompareString implements java.util.Comparator<String> {

  5. @Override

  6. public int compare(String o1, String o2) {

  7. int len1 = o1.length();

  8. int len2 = o2.length();

  9. return -(len1-len2);//需要按照降序排列

  10. }

  11. }

  12.  

比较 字符串的长度,按照 降序排列

 
  1. public static void main(String[] args) {

  2. List<String> list

  3. = new ArrayList<String>();

  4. list.add("abc");

  5. list.add("abcd");

  6. list.add("ab");

  7. list.add("abd");

  8. Collections.sort(list,new CompareString());

  9. System.out.println(list);

  10. //[abcd, abc, abd, ab]

  11. }

比如 商品,我需要按照价格的降序排列,代码如下:
商品 po类

 
  1. /**

  2. * 商品po类

  3. */

  4. public class Products {

  5. private String title;

  6. private int price;

  7. public Products() {}

  8. public Products(String title, int price) {

  9. super();

  10. this.title = title;

  11. this.price = price;

  12. }

  13.  
  14. public String getTitle() {

  15. return title;

  16. }

  17. public void setTitle(String title) {

  18. this.title = title;

  19. }

  20. public int getPrice() {

  21. return price;

  22. }

  23. public void setPrice(int price) {

  24. this.price = price;

  25. }

  26. @Override

  27. public String toString() {

  28. return "title=" + title+",price=" + price +"\n";

  29. }

  30. }

定义比较规则:

 
  1. /**

  2. * 按照价格的降序排列

  3. */

  4. public class ProductCompare implements java.util.Comparator<Products> {

  5. @Override

  6. public int compare(Products o1, Products o2) {

  7. return -( o1.getPrice()-o2.getPrice()>0?1: (o1.getPrice()==o2.getPrice()?0:-1));

  8. }

  9. }

数据比较:

 
  1. public static void main(String[] args) {

  2. List<Products> product

  3. = new ArrayList<Products>();

  4. product.add(new Products("a",120));

  5. product.add(new Products("b",143432));

  6. product.add(new Products("c",1892));

  7. product.add(new Products("d",11092));

  8. Collections.sort(product,new ProductCompare());

  9. System.out.println(product);

  10. 结果:

  11. [title=b,price=143432

  12. title=d,price=11092

  13. title=c,price=1892

  14. title=a,price=120

  15. ]

  16.  
  17. }

猜你喜欢

转载自blog.csdn.net/renjingjingya0429/article/details/82854342
今日推荐