List集合中对数据实现多重规则进行排序的案例

所属分类: 软件编程 / java 阅读数: 28
收藏 0 赞 0 分享

List集合进行排序时,很多人会考虑冒泡、快速等排序算法,但是对于多重排序规则的话,算法就不太适用了。其实java.util.Collections已经提供了sort的排序方法,并且能自己实现其排序规则。

现在有个场景:我需要对一批优惠券进行排序,优惠券有三个属性:是否可用、券类型、面额。我需要将可用的、券类型最大的、面额最大的券排到最前面。

即优先按是否可用排序,其次是券类型,再者就是面额。    

话不多说,看代码吧:

package com.test;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
 * List多重规则排序测试类
 */
public class TestCompartor {
  public static void main(String[] args) {
    ArrayList<Coupon> persons = new ArrayList<Coupon>();
    persons.add(new Coupon(13,0,new BigDecimal(40)));
    persons.add(new Coupon(13,0,new BigDecimal(50)));
    persons.add(new Coupon(13,0,new BigDecimal(45)));
    persons.add(new Coupon(1,0,new BigDecimal(20)));
    persons.add(new Coupon(13,1,new BigDecimal(30)));
    persons.add(new Coupon(1,0,new BigDecimal(25)));
    persons.add(new Coupon(11,0,new BigDecimal(50)));
    persons.add(new Coupon(11,1,new BigDecimal(40)));
    System.out.println("排序之前:");
    for (int i = 0; i <persons.size(); i++) {
      System.out.println(persons.get(i));
    }
    System.out.println();
    Collections.sort(persons, new Comparator<Coupon>() {
      //按可用升序,券类型降序,面额降序
      public int compare(Coupon o1, Coupon o2) {
        if (o1.valueAble.compareTo(o2.valueAble)==0){
         if(o2.themeType.compareTo(o1.themeType)==0){
         return o2.amount.compareTo(o1.amount)>0?1:-1;
         }else{
         return o2.themeType - o1.themeType;
         }
        }else{
          return o1.valueAble-o2.valueAble ;
        }
      }
    });
    System.out.println("排序后结果:");
    for (int i = 0; i <persons.size(); i++) {
      System.out.println(persons.get(i));
    }
  }
  static class Coupon{
    public Integer themeType; //优惠券类型
    public Integer valueAble; //可用 ,0 可用,1不可用
    public BigDecimal amount; //面额
    @Override
    public String toString() {
      return "Person{" +
          "themeType=" + themeType +
          ", valueAble=" + valueAble +
          ", amount=" + amount +
          '}';
    }
 public Coupon(Integer themeType, Integer valueAble, BigDecimal amount) {
  super();
  this.themeType = themeType;
  this.valueAble = valueAble;
  this.amount = amount;
 }
  }
}

至于封装工具类我就懒得弄了,有需要的自己封装吧。

这里如果用了Integer等封装类型,最好自己也做下非空处理。

排序之前:

Person{themeType=13, valueAble=0, amount=40} Person{themeType=13, valueAble=0, amount=50} Person{themeType=13, valueAble=0, amount=45} Person{themeType=1, valueAble=0, amount=20} Person{themeType=13, valueAble=1, amount=30} Person{themeType=1, valueAble=0, amount=25} Person{themeType=11, valueAble=0, amount=50} Person{themeType=11, valueAble=1, amount=40} 

排序后结果:

Person{themeType=13, valueAble=0, amount=50} Person{themeType=13, valueAble=0, amount=45} Person{themeType=13, valueAble=0, amount=40} Person{themeType=11, valueAble=0, amount=50} Person{themeType=1, valueAble=0, amount=25} Person{themeType=1, valueAble=0, amount=20} Person{themeType=13, valueAble=1, amount=30} Person{themeType=11, valueAble=1, amount=40}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

更多精彩内容其他人还在看

Spring Boot 配置 IDEA和DevTools 热部署的方法

这篇文章主要介绍了Spring Boot 配置 IDEA和DevTools 热部署的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringBoot使用Redis缓存的实现方法

这篇文章主要介绍了SpringBoot使用Redis缓存的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringBoot中自定义参数绑定步骤详解

这篇文章主要介绍了SpringBoot中自定义参数绑定步骤详解,非常不错,具有参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Java实现abc字符串排列组合

这篇文章主要为大家详细介绍了JAVA实现abc字符串的排列组合,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java中后台线程实例解析

这篇文章主要介绍了Java中后台线程实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

ehcache模糊批量移除缓存的方法

本篇文章主要介绍了ehcache模糊批量移除缓存的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java多线程join方法实例代码

这篇文章主要介绍了Java多线程join方法实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现字符串排列组合问题

这篇文章主要为大家详细介绍了java实现字符串排列组合问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java排列组合字符串的方法

这篇文章主要介绍了Java排列组合字符串的方法
收藏 0 赞 0 分享

Java语言中的自定义类加载器实例解析

这篇文章主要介绍了Java语言中的自定义类加载器实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多