Java之——利用Comparator接口对多个排序条件进行处理
转载自:http://blog.csdn.net/l1028386804/article/details/56513205
一、需求
假设现在有个如此的需求:需要对一个这样的雇员列表进行排序,排序规则如下:
1、首先级别最高的排在前面,
2、如果级别相等,那么按工资排序,工资高的排在前面,
3、如果工资相当则按入职年数排序,入职时间最长的排在前面。
雇员对象包含级别、工资和入职年份,代码如下:
- package import /**
- * 雇员信息
- * @author liuyazhuang
- *
- */ publicclassimplements privatestaticfinallong * ID
- */ publicint * 级别
- */ publicint * 工资
- */ publicint * 入职年数
- */ publicint publicint return publicvoidint this publicint return publicvoidint this publicint return publicvoidint this publicint return publicvoidint this publicintintintint this this this this }
二、实现Comparator接口
这里我们实现Java.util.Comparator接口,用于对雇员列表进行排序,代码如下:
- package import import /**
- * 核心排序类
- * @author liuyazhuang
- *
- */ publicclassimplements
- publicint int;
- int if) {
- ) ? : -;
- else
- if) {
- ) ? : -;
- else
- if) {
- ) ? : -;
- return }
三、验证排序结果
- package import import import import import import /**
- * 测试排序类
- *
- * @author liuyazhuang
- *
- */ publicclass
- publicvoidthrows new new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new );
- );
- for , employee.getId(), employee.getLevel(), employee.getSalary(),
- );
- }
运行结果:
四、附录
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */ package /**
- * A {@code Comparator} is used to compare two objects to determine their ordering with
- * respect to each other. On a given {@code Collection}, a {@code Comparator} can be used to
- * obtain a sorted {@code Collection} which is <i>totally ordered</i>. For a {@code Comparator}
- * to be <i>consistent with equals</i>, its {code #compare(Object, Object)}
- * method has to return zero for each pair of elements (a,b) where a.equals(b)
- * holds true. It is recommended that a {@code Comparator} implements
- * {@link java.io.Serializable}.
- *
- * @since 1.2
- */ publicinterface * Compares the two specified objects to determine their relative ordering. The ordering
- * implied by the return value of this method for all possible pairs of
- * {@code (lhs, rhs)} should form an <i>equivalence relation</i>.
- * This means that
- * <ul>
- * <li>{@code compare(a,a)} returns zero for all {@code a}</li>
- * <li>the sign of {@code compare(a,b)} must be the opposite of the sign of {@code
- * compare(b,a)} for all pairs of (a,b)</li>
- * <li>From {@code compare(a,b) > 0} and {@code compare(b,c) > 0} it must
- * follow {@code compare(a,c) > 0} for all possible combinations of {@code
- * (a,b,c)}</li>
- * </ul>
- *
- * @param lhs
- * an {@code Object}.
- * @param rhs
- * a second {@code Object} to compare with {@code lhs}.
- * @return an integer < 0 if {@code lhs} is less than {@code rhs}, 0 if they are
- * equal, and > 0 if {@code lhs} is greater than {@code rhs}.
- * @throws ClassCastException
- * if objects are not of the correct type.
- */ publicint * Compares this {@code Comparator} with the specified {@code Object} and indicates whether they
- * are equal. In order to be equal, {@code object} must represent the same object
- * as this instance using a class-specific comparison.
- * <p>
- * A {@code Comparator} never needs to override this method, but may choose so for
- * performance reasons.
- *
- * @param object
- * the {@code Object} to compare with this comparator.
- * @return boolean {@code true} if specified {@code Object} is the same as this
- * {@code Object}, and {@code false} otherwise.
- * @see Object#hashCode
- * @see Object#equals
- */ publicboolean }
转载于:https://www.cnblogs.com/Free-Thinker/p/10654436.html
Java之——利用Comparator接口对多个排序条件进行处理相关推荐
- Java Lambda简化Comparator接口匿名内部类写法
Lambda简化Comparator接口匿名内部类写法 //Lambda简化Comparator接口匿名内部类写法public static void main(String[] args) {Lis ...
- 【LeetCode】two num 利用comparable接口 对对象进行排序
题目two num 题意:给定一个整数数组和一个目标值.要求在数组中找到两个数.使得它们的和相加等于目标值.而且返回两个数的下标 思路:1.假设使用暴力,时间复杂度为O(n^2) 2.能够先将全部数进 ...
- JAVA ArrayList利用Comparator实现自定义排序
先说遇到的问题吧,,,ArrayList<String> 里面存放的本地文件路径,然后通过这个路径找到文件,按照文件的最后修改时间排序,,,,提起来没啥问题的,.,,当时写的代码这样的: ...
- JAVA如何利用迅雷接口解析磁力,关于神秘代码(磁力链接)如何正确使用
原标题:关于神秘代码(磁力链接)如何正确使用 神秘代码,也就是磁力链接 也许你对dd4486a844ac0e32a1b2f843e6492af429e34f29这种字符比较疑惑,但这就是磁力链接 (该 ...
- java排序接口_详解JAVA使用Comparator接口实现自定义排序
1.原则 Comparator接口可以实现自定义排序,实现Comparator接口时,要重写compare方法: int compare(Object o1, Object o2) 返回一个基本类型的 ...
- Java 的 Comparator 接口详解
Comparator 接口产生背景是排序 默认的 compareTo 方法是比较 字符码值大小: 如果我们需要实现比较字符的长度比较排序:在 Arrays.sort(数组对象,一个实现了Compara ...
- java Comparator接口使用详解
Comparator接口中有一个方法int compare(T o1, T o2).这个方法返回值是int类型,如果返回值小于0,说明比较结果是o1<o2,如果返回值等于0,说明比较结果是o1= ...
- java中sort函数comparator的使用_Comparator 与排序
Java 排序 Java 中经常需要对一个列表进行排序,列表中可能是基本数据类型,也可能是自定义对象,对于自定义对象的排序我们可能只想按照其某个属性排序,甚至多种条件组合对其排序,这些都可以借助于 C ...
- 使用Comparable、Comparator接口实现对对象数组、List集合自定义排序
1.实现对象数组排序 (1)方法一,需要排序的对象所属的类实现Comparable接口,复写 comparaTo方法 (2)方法二,需要排序的对象所属的类已经完成无法实现Comparable接口,这 ...
最新文章
- 抽点时间让我们一起来学linux系统
- mysql 错误1930xc1_Mysql写入记录出现 Incorrect string value: '\xB4\xE7\xB1\xCA\xBC\xC7‘错误?(写入中文)...
- 清除XCode缓存和生成文件
- linux驱动篇之 driver_register 过程分析(二)bus_add_driver
- 2020-11-12(JNI开发常见错误)
- 数据类型及pythop运算符号
- k8s部署nacos集群:3个pod、连接外部mysql读写分离集群
- linux 部署项目
- jpa 查询集合_避免懒惰的JPA集合
- Oracle15001,Oracle11gR2RAC环境DBCA创建数据库报错ORA-15055ORA-15001
- Asp.net 自定义config文件读取
- 高等数学基础 - 高等数学主要内容
- 展示面-关于wireshark抓包总结
- 并行、并发和代码性能优化
- Windows下搭建Octopress博客
- java socket5源码_Java利用TCP协议实现客户端与服务器通信【附通信源码】
- python数据建模工具_python数据分析工具——Pandas、StatsModels、Scikit-Learn
- VMware许可证过期解决方案
- 智能时代如何构建金融反欺诈体系?
- 零基础CSS入门教程(28)–CSS导航栏实例
热门文章
- [转]C#中使用Monitor类、Lock和Mutex类来同步多线程的执行
- python初学者web还是爬虫-还在纠结学爬虫还是数据分析,不如看看这篇文章
- python白名单验证-Python中XSS白名单过滤的实现
- python画动态爱心-使用Python画出小人发射爱心的代码
- python散点图拟合曲线-python 绘制拟合曲线并加指定点标识的实现
- 零基础如何学python-零基础小白如何学python,想请教大家,求指导 ?
- python 爬虫源代码-Python爬虫学习之获取指定网页源码
- 学会python编程容易吗-Python编程入门难不难
- python英语单词-干货|3天教你掌握Python必备常用英语词汇
- python语言教程-python基础教程