前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【编程基础】Java Comparator接口的使用

【编程基础】Java Comparator接口的使用

作者头像
程序员互动联盟
发布2018-03-14 16:12:35
7500
发布2018-03-14 16:12:35
举报
在实际编程中我们经常会用到集合或者数组,有的时候你需要对这个集合中的元素就行排序,那这个时候就用到了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]

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-10-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序员互动联盟 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档