基于Comparator对象集合实现多个条件按照优先级的比较

 更新时间:2021年07月12日 09:25:41   作者:CoderYin  
这篇文章主要介绍了基于Comparator对象集合实现多个条件按照优先级的比较,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

一、背景介绍

在日常的java开发中,我们在返回一个对象集合时需要按照对象的某个属性或者某些属性进行排序返回给前端进行展示,例如我最近需要返回一个题库集合,需要先根据指定时间排序然后根据创建时间进行排序,在mysql层进行操作比较麻烦而且浪费时间,我们可以通过程序来进行排序。

二、案例代码

// 实体类
public class People {
	private Integer id;
	private String name;
	private Integer topTime;// 置顶时间
	private Integer gmtCreate;// 创建时间 
	public People(Integer id, String name, Integer topTime, Integer gmtCreate) {
		super();
		this.id = id;
		this.name = name;
		this.topTime = topTime;
		this.gmtCreate = gmtCreate;
	}
 
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getTopTime() {
		return topTime;
	}
	public void setTopTime(Integer topTime) {
		this.topTime = topTime;
	}
	public Integer getGmtCreate() {
		return gmtCreate;
	}
	public void setGmtCreate(Integer gmtCreate) {
		this.gmtCreate = gmtCreate;
	}
 
	@Override
	public String toString() {
		return "People [id=" + id + ", name=" + name + ", topTime=" + topTime + ", gmtCreate=" + gmtCreate + "]";
	}
}
// 排序方法
public class PeopleComparator implements Comparator<People>{
 
	@Override
	public int compare(People o1, People o2) {
		int result = 0;
		// 按照置顶时间排序升序(o1,o2位置互换就是降序)
		int topTimeSeq = o2.getTopTime() - o1.getTopTime();
		if(topTimeSeq != 0){
			result = (topTimeSeq > 0) ? 3 : -1;
		}else{
			// 按照创建时间排序
			topTimeSeq = o2.getGmtCreate() - o1.getGmtCreate();
			if(topTimeSeq != 0){
				result = (topTimeSeq > 0) ? 2 : -2;
			}
		}
		return result;
	}
}
// 测试
public class PeopleTest {
	public static void main(String[] args) {
		List<People> peopleList = new ArrayList<People>(){
			{
				add(new People(1,"tom1",0,1));
				add(new People(2,"tom2",2,4));
				add(new People(3,"tom3",1,3));
				add(new People(4,"tom4",0,6));
				add(new People(5,"tom5",0,2));
				add(new People(6,"tom6",0,5));
			}
		};
		Collections.sort(peopleList,new PeopleComparator());
		for(People p:peopleList){
			System.out.println(p.toString());
		}
	}
}

测试结果

Comparator 多条件比较

class Card {
    int a;
    int b;
    public Card(int a, int b) {
        this.a = a;
        this.b = b;
    }
    public int getA() {
        return a;
    }
    public int getB() {
        return b;
    }
    @Override
    public String toString() {
        return "Card{" +
                "a=" + a +
                ", b=" + b +
                '}';
    }
}
public class Main {
    public static void main(String[] args) {
        List<Card> list = new ArrayList<>();
        list.add(new Card(0, 2));
        list.add(new Card(1, 1));
        list.add(new Card(1, 0));
        list.add(new Card(1, 0));
        list.add(new Card(2, 0));
        System.out.println(list);
        System.out.println();
        
        Collections.sort(list, new Comparator<Card>() {
            @Override
            public int compare(Card c1, Card c2) {
                // c1 - c2  升序
                // c2 - c1  降序
                int res1 = c2.b - c1.b;
                int res2 = c2.a - c1.a;
                // 当 b相等时比较a, 否则先比较b
                return res1 == 0 ? res2 : res1;
            }
        });
        System.out.println(list);
    }
}
[Card{a=0, b=2}, Card{a=1, b=1}, Card{a=1, b=0}, Card{a=1, b=0}, Card{a=2, b=0}]
[Card{a=0, b=2}, Card{a=1, b=1}, Card{a=2, b=0}, Card{a=1, b=0}, Card{a=1, b=0}]

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 贪心算法原理及在Java中的使用

    贪心算法原理及在Java中的使用

    我们可能在好多地方都会听到贪心算法这一概念,并且它的算法思想也比较简单就是说算法只保证局部最优,进而达到全局最优。但我们实际编程的过程中用的并不是很多,究其原因可能是贪心算法使用的条件比较苛刻,所要解决的问题必须满足贪心选择性质
    2021-05-05
  • CountDownLatch基于AQS阻塞工具用法详解

    CountDownLatch基于AQS阻塞工具用法详解

    这篇文章主要为大家介绍了CountDownLatch基于AQS阻塞工具用法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • Java中的forEach循环详细解读

    Java中的forEach循环详细解读

    这篇文章主要介绍了Java中的forEach循环详细解读,不要再foreach循环里面进行元素的add和remove,如果你非要进行remove元素,那么请使用Iterator方式,如果存在并发,那么你一定要选择加锁,需要的朋友可以参考下
    2023-12-12
  • Java中Servlet的生命周期详解

    Java中Servlet的生命周期详解

    这篇文章主要介绍了Java中Servlet的生命周期详解,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • 详解使用Spring的restTemplete进行Http请求

    详解使用Spring的restTemplete进行Http请求

    本篇文章主要介绍了详解使用Spring的restTemplete进行Http请求,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • SpringBoot中的自定义starter

    SpringBoot中的自定义starter

    这篇文章主要介绍了SpringBoot中的自定义starter,Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境(条件)进行自动配置,需要的朋友可以参考下
    2024-01-01
  • springboot集成测试容器重启问题的处理

    springboot集成测试容器重启问题的处理

    这篇文章主要介绍了springboot集成测试容器重启问题的处理,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • shiro 认证流程操作

    shiro 认证流程操作

    这篇文章主要介绍了shiro 认证操作的相关资料,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-01-01
  • Java日常练习题,每天进步一点点(27)

    Java日常练习题,每天进步一点点(27)

    下面小编就为大家带来一篇Java基础的几道练习题(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望可以帮到你
    2021-07-07
  • SpringBoot入门之集成Druid的方法示例

    SpringBoot入门之集成Druid的方法示例

    这篇文章主要介绍了SpringBoot入门之集成Druid的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07

最新评论