jdk之Comparator
来源:互联网 发布:网络用语奥义很爽 编辑:程序博客网 时间:2024/05/16 11:31
comparator 是javase中的接口,位于java.util包下,该接口表示可以比较。
典型的运用是数组工具类和集合工作类中都有含Comparator接口的sort重载方法。
jdk源码:
public interface Comparator<T> { /** * Compares its two arguments for order. Returns a negative integer, * zero, or a positive integer as the first argument is less than, equal * to, or greater than the second.<p> * * In the foregoing description, the notation * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical * <i>signum</i> function, which is defined to return one of <tt>-1</tt>, * <tt>0</tt>, or <tt>1</tt> according to whether the value of * <i>expression</i> is negative, zero or positive.<p> * * The implementor must ensure that <tt>sgn(compare(x, y)) == * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This * implies that <tt>compare(x, y)</tt> must throw an exception if and only * if <tt>compare(y, x)</tt> throws an exception.)<p> * * The implementor must also ensure that the relation is transitive: * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies * <tt>compare(x, z)>0</tt>.<p> * * Finally, the implementor must ensure that <tt>compare(x, y)==0</tt> * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all * <tt>z</tt>.<p> * * It is generally the case, but <i>not</i> strictly required that * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking, * any comparator that violates this condition should clearly indicate * this fact. The recommended language is "Note: this comparator * imposes orderings that are inconsistent with equals." * * @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); /** * Indicates whether some other object is "equal to" this * comparator. This method must obey the general contract of * {@link Object#equals(Object)}. Additionally, this method can return * <tt>true</tt> <i>only</i> if the specified object is also a comparator * and it imposes the same ordering as this comparator. Thus, * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1, * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference * <tt>o1</tt> and <tt>o2</tt>.<p> * * Note that it is <i>always</i> safe <i>not</i> to override * <tt>Object.equals(Object)</tt>. However, overriding this method may, * in some cases, improve performance by allowing programs to determine * that two distinct comparators impose the same order. * * @param obj the reference object with which to compare. * @return <code>true</code> only if the specified object is also * a comparator and it imposes the same ordering as this * comparator. * @see Object#equals(Object) * @see Object#hashCode() */ boolean equals(Object obj);}
可以看出Comparator声明了两个方法equals和compare两个方法
事例说明:
用户实体:
package com.sun.utils.comparator;public class User {public User(String name, Long id) {super();this.name = name;this.id = id;}private String name;private Long id;public String getName() {return name;}public void setName(String name) {this.name = name;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}@Overridepublic String toString() {return "User [name=" + name + ", id=" + id + "]";}}比较器:
如果u1比u2大则返回大于0的值
u1等于u2则返回0
u1小于u2则返回小于0的值
package com.sun.utils.comparator;import java.util.Comparator;public class TestComparator implements Comparator<User>{public int compare(User u1, User u2) {// TODO Auto-generated method stubif(u1.getId()>u2.getId()) {return 1;} else if(u1.getId()<u2.getId()) {return -1;}return 0;}}
运行类:
package com.sun.utils.comparator;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class RunTest{public static void main(String[] args) { List<User> list= new ArrayList<>(); list.add(new User("zxx", 2l)); list.add(new User("www", 1l)); TestComparator comparator = new TestComparator(); Collections.sort(list, comparator); System.out.println(list);}}
打印结果:
[User [name=www, id=1], User [name=zxx, id=2]]
从比较结果可以看出sort方法会根据comparator的compare方法进行排序
阅读全文
0 0
- jdk之Comparator
- Java之Comparable,Comparator
- 策略模式----之Comparator
- common-io之Comparator阅读
- common-io之Comparator阅读
- Java之Comparable和Comparator
- Comparable与Comparator之比较
- java之Comparator与Comparable
- Apache commons 之 Collections :Comparator
- 科大讯飞之Comparator比较器
- comparator
- Comparator
- comparator
- Comparator
- JDK学习---深入理解Comparator、TreeSet、TreeMap为什么可以排序
- Comparable和Comparator之对象比较
- Comparable与Comparator之区别与使用
- Java 7之基础 - Comparable与Comparator
- Java注解(转载)
- IEEE-754 浮點數的表示法
- 数据库三大范式详解
- 表单提交---内部服务器错误
- 请实现一个算法,确定一个字符串的所有字符是否全都不同。这里我们要求不允许使用额外的存储结构。
- jdk之Comparator
- MAC JDK默认安装路径 JAVA路径
- codeforces 331D 河狸boshi 【区间dp+期望】
- C++实现对每行数据的分段截取
- spring 注入例子
- 20170704
- 如何用react+react-router+fetch+webpack2简单集成一个移动端项目
- socket的accept函数解析
- 计算机网络