【编程基础】JavaComparator接口的使用


在实际编程中我们经常会用到集合或者数组,有的时候你需要对这个集合中的元素就行排序,那这个时候就用到了Comparator接口,先看一下接口的原型:

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); }

函数说明:

1、若一个类要实现Comparator接口,那么这个类一定要实现它的两个方法compareTo(T o1, T o2)和equals(Object obj);

2、int compareTo(T o1, T o2)方法的返回值决定了比较的顺序,看你具体是怎么实现的,o1大于o2返回正数,o1等于o2返回0,o1小于o2返回负数;

3、equals(Object obj)方法可以空着,因为任何类默认已经实现了equals(Object obj)方法;

如果我们要对某个对象进行排序我们可以建一个该类的比较器,比较的规则可以自己制定,比如:

public class Student { int age; String name; Student(int age, String name) { this.age = age; this.name = name; } public static void main(String[] arg) { ArrayList<Student> students = new ArrayList<Student>(); students.add(new Student(23, "dd")); students.add(new Student(22, "cc")); students.add(new Student(22, "bb")); students.add(new Student(25, "aa")); Collections.sort(students, new StudentComparator()); System.out.print(students); } @Override public String toString() { return " age = " + age + " name = " + name; } } class StudentComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { return (o1.age - o2.age); } }

上面的比较器是按照学生的年龄大小进行比较,这个比较的规则你可以自定义,你也可以按照名字来比较,上面的程序运行结果是:

[ age = 22 name = cc, age = 22 name = bb, age = 23 name = dd, age = 25 name = aa]

优质内容筛选与推荐>>
1、CoffeeScript的类继承的工具函数extends
2、SVG 基本绘图方法总结
3、Send mail from PL/SQL-from cyber
4、机器学习的常用算法 --- 2016/7/19
5、Last working day of 2012, post一些Windbg的命令。


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号