我的Java开发学习之旅------>Java利用Comparator接口对多个排序条件进行处理

简介: 一需求二实现Comparator接口三验证排序结果验证第一条件首先按级别排序级别最高的排在前面验证第二条如果级别相等那么按工资排序工资高的排在前面验证第三条如果工资相当则按入职年数排序入职时间最长的排在前面附录javautilComparator接口源代码一、需求 假设现在有个如此的需求:需要对一个这样的雇员列表进行排序,排序规则如下: 1、首先级别最高的排在前面, 2、如果级别相等,那么按工资排序,工资高的排在前面, 3、如果工资相当则按入职年数排序,入职时间最长的排在前面。

一、需求

假设现在有个如此的需求:需要对一个这样的雇员列表进行排序,排序规则如下:
1、首先级别最高的排在前面,
2、如果级别相等,那么按工资排序,工资高的排在前面,
3、如果工资相当则按入职年数排序,入职时间最长的排在前面。

雇员对象包含级别、工资和入职年份,代码如下:

     /**
     * 雇员
     */
    class Employee {
        /**
         * ID
         */
        public int id;
        /**
         * 级别
         */
        public int level;
        /**
         * 工资
         */
        public int salary;
        /**
         * 入职年数
         */
        public int year;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getLevel() {
            return level;
        }

        public void setLevel(int level) {
            this.level = level;
        }

        public int getSalary() {
            return salary;
        }

        public void setSalary(int salary) {
            this.salary = salary;
        }

        public int getYear() {
            return year;
        }

        public void setYear(int year) {
            this.year = year;
        }

        public Employee(int id, int level, int salary, int year) {
            this.id = id;
            this.level = level;
            this.salary = salary;
            this.year = year;
        }
    }

二、实现Comparator接口

这里我们实现java.util.Comparator接口,用于对雇员列表进行排序,代码如下:

private Comparator<Employee> comparator = new Comparator<Employee>() {
        @Override
        public int compare(Employee employee1, Employee employee2) {
            int cr = 0;
            //按级别降序排列
            int a = employee2.getLevel() - employee1.getLevel();
            if (a != 0) {
                cr = (a > 0) ? 3 : -1;
            } else {
                //按薪水降序排列
                a = employee2.getSalary() - employee1.getSalary();
                if (a != 0) {
                    cr = (a > 0) ? 2 : -2;
                } else {
                    //按入职年数降序排列
                    a = employee2.getYear() - employee1.getYear();
                    if (a != 0) {
                        cr = (a > 0) ? 1 : -3;
                    }
                }
            }
            return cr;
        }
    };

三、验证排序结果

下面用一个单元测试,来验证排序结果是否正确

@Test
public void sortTest() throws Exception {
    List<Employee> employeeList = new ArrayList<Employee>() {{
        add(new Employee(1, 9, 10000, 10));
        add(new Employee(2, 9, 12000, 7));
        add(new Employee(3, 5, 10000, 12));
        add(new Employee(4, 5, 10000, 6));
        add(new Employee(5, 3, 5000, 3));
        add(new Employee(6, 1, 2500, 1));
        add(new Employee(7, 5, 8000, 10));
        add(new Employee(8, 3, 8000, 2));
        add(new Employee(9, 1, 3000, 5));
        add(new Employee(10, 1, 2500, 4));
        add(new Employee(11, 2, 2000, 4));
    }};
    Collections.sort(employeeList, comparator);
    System.out.println("ID\tLevel\tSalary\tYears");
    System.out.println("=============================");
    for (Employee employee : employeeList) {
        System.out.printf("%d\t%d\t%d\t%d\n", employee.getId(), employee.getLevel(), employee.getSalary(), employee.getYear());
    }
    System.out.println("=============================");
}

整个完整代码如下所示:

/**
 *  1、首先级别最高的排在前面,<br/>
    2、如果级别相等,那么按工资排序,工资高的排在前面,<br/>
    3、如果工资相当则按入职年数排序,入职时间最长的排在前面。<br/>
 *<p/>
 *created by OuyangPeng on 2016/8/2. <a href="http://blog.csdn.net/ouyang_peng">http://blog.csdn.net/ouyang_peng</a>
 */
public class SortTest2 {
    /**
     * 雇员
     */
    class Employee {
        /**
         * ID
         */
        public int id;
        /**
         * 级别
         */
        public int level;
        /**
         * 工资
         */
        public int salary;
        /**
         * 入职年数
         */
        public int year;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getLevel() {
            return level;
        }

        public void setLevel(int level) {
            this.level = level;
        }

        public int getSalary() {
            return salary;
        }

        public void setSalary(int salary) {
            this.salary = salary;
        }

        public int getYear() {
            return year;
        }

        public void setYear(int year) {
            this.year = year;
        }

        public Employee(int id, int level, int salary, int year) {
            this.id = id;
            this.level = level;
            this.salary = salary;
            this.year = year;
        }
    }

    private Comparator<Employee> comparator = new Comparator<Employee>() {
        @Override
        public int compare(Employee employee1, Employee employee2) {
            int cr = 0;
            //按级别降序排列
            int a = employee2.getLevel() - employee1.getLevel();
            if (a != 0) {
                cr = (a > 0) ? 3 : -1;
            } else {
                //按薪水降序排列
                a = employee2.getSalary() - employee1.getSalary();
                if (a != 0) {
                    cr = (a > 0) ? 2 : -2;
                } else {
                    //按入职年数降序排列
                    a = employee2.getYear() - employee1.getYear();
                    if (a != 0) {
                        cr = (a > 0) ? 1 : -3;
                    }
                }
            }
            return cr;
        }
    };

    @Test
    public void sortTest() throws Exception {
        List<Employee> employeeList = new ArrayList<Employee>() {{
            add(new Employee(1, 9, 10000, 10));
            add(new Employee(2, 9, 12000, 7));
            add(new Employee(3, 5, 10000, 12));
            add(new Employee(4, 5, 10000, 6));
            add(new Employee(5, 3, 5000, 3));
            add(new Employee(6, 1, 2500, 1));
            add(new Employee(7, 5, 8000, 10));
            add(new Employee(8, 3, 8000, 2));
            add(new Employee(9, 1, 3000, 5));
            add(new Employee(10, 1, 2500, 4));
            add(new Employee(11, 2, 2000, 4));
        }};
        Collections.sort(employeeList, comparator);
        System.out.println("ID\tLevel\tSalary\tYears");
        System.out.println("=============================");
        for (Employee employee : employeeList) {
            System.out.printf("%d\t%d\t%d\t%d\n", employee.getId(), employee.getLevel(), employee.getSalary(), employee.getYear());
        }
        System.out.println("=============================");
    }
}

运行结果:

ID  Level   Salary  Years
=============================
2   9   12000   7
1   9   10000   10
3   5   10000   12
4   5   10000   6
7   5   8000    10
8   3   8000    2
5   3   5000    3
11  2   2000    4
9   1   3000    5
10  1   2500    4
6   1   2500    1
=============================

验证第一条件:首先按级别排序,级别最高的排在前面

从上面的运行结果可以发现,还是满足需求第一条要求的:首先按级别排序,级别最高的排在前面
1、首先从整体来看,是从级别最高的9级排序到级别最低的1级

ID  Level   Salary  Years
=============================
2   9   12000   7
1   9   10000   10
3   5   10000   12
4   5   10000   6
7   5   8000    10
8   3   8000    2
5   3   5000    3
11  2   2000    4
9   1   3000    5
10  1   2500    4
6   1   2500    1
=============================

验证第二条:如果级别相等,那么按工资排序,工资高的排在前面

2、当级别相同的情况下,如下两天记录:

2   9   12000   7
1   9   10000   10

则都是9级,这个时候是满足第二条要求的:如果级别相等,那么按工资排序,工资高的排在前面

下面的3条记录也是满足第二条要求的:

9   1   3000    5
10  1   2500    4
6   1   2500    1

则都是1级,工资为3000的排在工资为2500的前面。

验证第三条:如果工资相当则按入职年数排序,入职时间最长的排在前面

3、当级别和工资都相同的情况下,则比较入职年数。如下面的两条记录

10  1   2500    4
6   1   2500    1

级别都是1级,工资都是2500,年数为4的排在年数为1的前面。这个时候是满足第三条要求的:如果工资相当则按入职年数排序,入职时间最长的排在前面


其实Comparator接口直接写成下面的就行了

private Comparator<Employee> comparator = new Comparator<Employee>() {
        @Override
        public int compare(Employee employee1, Employee employee2) {
            int cr = 0;
            //按级别降序排列
            int a = employee2.getLevel() - employee1.getLevel();
            if (a != 0) {
                cr = (a > 0) ? 1 : -1;
            } else {
                //按薪水降序排列
                a = employee2.getSalary() - employee1.getSalary();
                if (a != 0) {
                    cr = (a > 0) ? 1 : -1;
                } else {
                    //按入职年数降序排列
                    a = employee2.getYear() - employee1.getYear();
                    if (a != 0) {
                        cr = (a > 0) ? 1 : -1;
                    }
                }
            }
            return cr;
        }
    };

附录: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 java.util;

/**
 * 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
 */
public interface Comparator<T> {
    /**
     * 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.
     */
    public int compare(T lhs, T rhs);

    /**
     * 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
     */
    public boolean equals(Object object);
}

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng

这里写图片描述

相关文章
|
5天前
|
数据采集 JSON Java
利用Java获取京东SKU接口指南
本文介绍如何使用Java通过京东API获取商品SKU信息。首先,需注册京东开放平台账号并创建应用以获取AppKey和AppSecret。接着,查阅API文档了解调用方法。明确商品ID后,构建请求参数并通过HTTP客户端发送请求。最后,解析返回的JSON数据提取SKU信息。注意遵守API调用频率限制及数据保护法规。此方法适用于电商平台及其他数据获取场景。
|
10天前
|
安全 Java API
java如何请求接口然后终止某个线程
通过本文的介绍,您应该能够理解如何在Java中请求接口并根据返回结果终止某个线程。合理使用标志位或 `interrupt`方法可以确保线程的安全终止,而处理好网络请求中的各种异常情况,可以提高程序的稳定性和可靠性。
40 6
|
27天前
|
Java API
Java中内置的函数式接口
Java中内置的函数式接口
25 2
|
1月前
|
Java
在Java中如何实现接口?
实现接口是 Java 编程中的一个重要环节,它有助于提高代码的规范性、可扩展性和复用性。通过正确地实现接口,可以使代码更加灵活、易于维护和扩展。
59 3
|
1月前
|
Java
在Java中,接口之间可以继承吗?
接口继承是一种重要的机制,它允许一个接口从另一个或多个接口继承方法和常量。
89 1
|
1月前
|
Java 开发者
在 Java 中,一个类可以实现多个接口吗?
这是 Java 面向对象编程的一个重要特性,它提供了极大的灵活性和扩展性。
139 57
|
1月前
|
Java
在Java中实现接口的具体代码示例
可以根据具体的需求,创建更多的类来实现这个接口,以满足不同形状的计算需求。希望这个示例对你理解在 Java 中如何实现接口有所帮助。
42 1
|
4月前
|
搜索推荐 算法 Java
|
4月前
|
搜索推荐 算法 Java
经典排序算法之-----选择排序(Java实现)
这篇文章通过Java代码示例详细解释了选择排序算法的实现过程,包括算法的基本思想、核心代码、辅助函数以及测试结果,展示了如何通过选择排序对数组进行升序排列。
经典排序算法之-----选择排序(Java实现)
|
6月前
|
存储 搜索推荐 算法
十大排序算法(java实现)(二)
十大排序算法(java实现)(二)