Java 之 Comparable 与 Comparator

Java中,Comparable、Comparator 接口 都是 用来 做比较的。

Comparable

Comparable 接口 位于 java.lang 包下,源码如下:

package java.lang;
import java.util.*;

/**
 * @param <T> the type of objects that this object may be compared to
 *
 * @author  Josh Bloch
 * @see java.util.Comparator
 * @since 1.2
 */
public interface Comparable<T> {
    /**
     * @param   o the object to be compared.
     * @return  a negative integer, zero, or a positive integer as this object
     *          is less than, equal to, or greater than the specified object.
     *
     * @throws NullPointerException if the specified object is null
     * @throws ClassCastException if the specified object's type prevents it
     *         from being compared to this object.
     */
    public int compareTo(T o);
}

Comparator

有时,无法对类进行修改,或者说 修改此类的成本太高,但是又希望对其进行排序,那怎么办?这个时候 Comparator 接口就派上用场了,Comparator 接口 位于 java.util 包下,部分源码 如下:

package java.util;

import java.io.Serializable;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.ToDoubleFunction;
import java.util.Comparators;

/**
 * @param <T> the type of objects that may be compared by this comparator
 *
 * @author  Josh Bloch
 * @author  Neal Gafter
 * @see Comparable
 * @see java.io.Serializable
 * @since 1.2
 */
@FunctionalInterface
public interface Comparator<T> {
    /**
     * @param o1 the first object to be compared.
     * @param o2 the second object to be compared.
     * @return a negative integer, zero, or a positive integer as the
     *         first argument is less than, equal to, or greater than the
     *         second.
     * @throws NullPointerException if an argument is null and this
     *         comparator does not permit null arguments
     * @throws ClassCastException if the arguments' types prevent them from
     *         being compared by this comparator.
     */
    int compare(T o1, T o2);
}

下面,举个简单的例子。代码如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Administrator on 2018/7/18 21:14 in Beijing.
 */

public class CompareTest {
    public static void main(String[] args) {
        EmployeeA employeeA1 = new EmployeeA("gsj_A", 20000);
        EmployeeA employeeA2 = new EmployeeA("pbz_A", 25000);
        EmployeeA employeeA3 = new EmployeeA("lz_A", 17000);

        List<EmployeeA> listA = new ArrayList<EmployeeA>();
        listA.add(employeeA1);
        listA.add(employeeA2);
        listA.add(employeeA3);

        System.out.println(listA);
        Collections.sort(listA);
        System.out.println(listA);

        System.out.println();

        EmployeeB employeeB1 = new EmployeeB("gsj_B", 20000);
        EmployeeB employeeB2 = new EmployeeB("pbz_B", 25000);
        EmployeeB employeeB3 = new EmployeeB("lz_B", 17000);

        List<EmployeeB> listB = new ArrayList<EmployeeB>();
        listB.add(employeeB1);
        listB.add(employeeB2);
        listB.add(employeeB3);

        System.out.println(listB);

        //JDK 8 以前
        Comparator<EmployeeB> comparator1 = new Comparator<EmployeeB>() {
            @Override
            public int compare(EmployeeB o1, EmployeeB o2) {
                return (int)(o1.getSalary() - o2.getSalary());
            }
        };
        Collections.sort(listB, comparator1);

        //JDK 8 之后
        Comparator<EmployeeB> comparator2 = (EmployeeB e1, EmployeeB e2) -> (int)(e1.getSalary() - e2.getSalary());
        Comparator<EmployeeB> comparator3 = Comparator.comparing(EmployeeB::getSalary);
        //Collections.sort(listB, comparator2);
        //Collections.sort(listB, comparator3);

        System.out.println(listB);
    }
}

class EmployeeA implements Comparable<EmployeeA> {
    private String name;
    private double salary;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

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

    public EmployeeA(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    @Override
    public int compareTo(EmployeeA other) {
        return (int)(this.salary - other.salary);
    }

    @Override
    public String toString() {
        return "name: " + name + "---salary: " + salary;
    }
}

final class EmployeeB {
    private String name;
    private double salary;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

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

    public EmployeeB(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "name: " + name + "---salary: " + salary;
    }
}

输出如下

[name: gsj_A---salary: 20000.0, name: pbz_A---salary: 25000.0, name: lz_A---salary: 17000.0]
[name: lz_A---salary: 17000.0, name: gsj_A---salary: 20000.0, name: pbz_A---salary: 25000.0]

[name: gsj_B---salary: 20000.0, name: pbz_B---salary: 25000.0, name: lz_B---salary: 17000.0]
[name: lz_B---salary: 17000.0, name: gsj_B---salary: 20000.0, name: pbz_B---salary: 25000.0]

猜你喜欢

转载自blog.csdn.net/yitengtongweishi/article/details/81117486