Java之——利用Comparator接口对多个排序条件进行处理
转载自:http://blog.csdn.net/l1028386804/article/details/56513205
一、需求
假设现在有个如此的需求:需要对一个这样的雇员列表进行排序,排序规则如下:
1、首先级别最高的排在前面,
2、如果级别相等,那么按工资排序,工资高的排在前面,
3、如果工资相当则按入职年数排序,入职时间最长的排在前面。
雇员对象包含级别、工资和入职年份,代码如下:
- package
import
/**
- * 雇员信息
- * @author liuyazhuang
- *
- */
publicclassimplementsprivatestaticfinallong
* ID
- */
publicint* 级别
- */
publicint* 工资
- */
publicint* 入职年数
- */
publicintpublicint
returnpublicvoidint
thispublicint
returnpublicvoidint
thispublicint
returnpublicvoidint
thispublicint
returnpublicvoidint
thispublicintintintint
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
importimport
import
import/**
- * 测试排序类
- *
- * @author liuyazhuang
- *
- */
publicclass - publicvoidthrows
newnew, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new, , , ));
- new
); - );
- for
, employee.getId(), employee.getLevel(), employee.getSalary(), - );
- }
运行结果:
四、附录
java.util.Comparator接口源代码
- /*
- * 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
}
Java之——利用Comparator接口对多个排序条件进行处理的更多相关文章
- 我的Java开发学习之旅------>Java利用Comparator接口对多个排序条件进行处理
一需求 二实现Comparator接口 三验证排序结果 验证第一条件首先按级别排序级别最高的排在前面 验证第二条如果级别相等那么按工资排序工资高的排在前面 验证第三条如果工资相当则按入职年数排序入职时 ...
- java Comparable 和 Comparator接口区别
Comparable 简介 Comparable 是排序接口. 若一个类实现了Comparable接口,就意味着“该类支持排序”. 即然实现Comparable接口的类支持排序,假设现在存在“实现C ...
- Java Comparable 和 Comparator 接口详解
本文基于 JDK8 分析 Comparable Comparable 接口位于 java.lang 包下,Comparable 接口下有一个 compareTo 方法,称为自然比较方法.一个类只要实现 ...
- 【LeetCode】two num 利用comparable接口 对对象进行排序
题目two num 题意:给定一个整数数组和一个目标值.要求在数组中找到两个数.使得它们的和相加等于目标值.而且返回两个数的下标 思路:1.假设使用暴力,时间复杂度为O(n^2) 2.能够先将全部数进 ...
- java利用Comparator接口对自定义数组排序
import java.util.Arrays; import java.util.Comparator; public class MySort { public static void main( ...
- java基础-Comparator接口与Collections实现排序算法
java 排序Comparable和Comparator使用 java提供了两个排序用的接口Comparable和Comparator,一般情况下使用区别如下: Comparable 接口用于类的固定 ...
- Java语言利用Collections.sort对Map,List排序
1.main方法包含TreeMap排序1,TreeMap排序2,HashMap排序,List<Integer>排序,List<Bean>排序,List<Map>排序 ...
- Java:Comparator接口
public interface Comparator<T> 接口里面的方法 int compare(T o1, T o2) o1 > o2 返回 1 o1 = o2 返回 0 o1 ...
- java中Comparatable接口和Comparator接口的区别
1.不同类型的排序规则 .自然排序是什么? 自然排序是一种升序排序.对于不同的数据类型,升序规则不一样: BigDecimal BigInteger Byte Double Float Int ...
随机推荐
- Golang面向对象_继承
package main import "fmt" type Person struct { name string //名字 sex byte //性别 age int //年龄 ...
- Python3 Pandas的DataFrame格式数据写入excle文件、json、html、剪贴板、数据库
Python3 Pandas的DataFrame格式数据写入excle文件.json.html.剪贴板.数据库 一.DataFrame格式数据 Pandas是Python下一个开源数据分析的库,它提供 ...
- pagehelper调用mybatis报错java.lang.NoSuchMethodError:org.apache.ibatis.reflection.MetaObject.forObject
最近在升新的基础框架到spring cloud,启动时pagehelper报错,如下: java.lang.NoSuchMethodError:org.apache.ibatis.reflection ...
- windows下openssl编译
昨晚,在编译rabbitmq-c时,使用cmake生成vs项目文件时遇到下列错误: CMake Error at C:/Program Files/CMake/share/cmake-3.6/Modu ...
- Angular4.x 创建组件|绑定数据|绑定属性|数据循环|条件判断|事件|表单处理|双向数据绑定
Angular4.x 创建组件|绑定数据|绑定属性|数据循环|条件判断|事件|表单处理|双向数据绑定 创建 angular 组件 https://github.com/angular/angular- ...
- 20145308 《网络对抗》 Web应用 学习总结
20145308 <网络对抗> Web应用 学习总结 实验内容 (1)Web前端HTML (2)Web前端javascipt (3)Web后端:MySQL基础:正常安装.启动MySQL,建 ...
- bzoj 2084 Antisymmetry - Manacher
题目传送门 需要高级权限的传送门 题目大意 对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一样,就称作“反对称”字符串. 问给定长度为$n$的一个01串有多少个子串是反对称的 ...
- luogu 3790 文艺数学题 - 矩阵树定理 - 容斥原理
题目传送门 戳我来传送 题目大意 给定一个图,问它的所有生成树的边权的最大公约数之和. 可以考虑计算边权的最大公约数为$i$的生成树的个数$f(i)$,最后累加答案. 然后考虑这样的生成树的个数怎么求 ...
- centos 7.2 安装gitlab汉化
####################你如果搜到我的这个博客,你的系统得是centos 7的 80端口没有占用. QQ:1394466404 这个博客维护1年 #### 多地方第一个是百度 ...
- Eclipse中在xml文件中,ctrl+左键的快捷键,点击class定位,不生效
修改方式: 第一种方式:Window -> Preferences -> General -> Editors -> File Associations ...