集合类API提供了一些通用的算法来操作所有的容器,这些都是Collections类中的静态方法(注意这是一个与Collection接口不同的类)。在Array中也有一些操作数组(排序,查找)的静态方法。泛型版的大部分这些方法都要被重载,每个基本类型(boolean除外)都要重载一次。
1.Comparator函数对象
许多集合类API类和例程都需要具有对对象进行排序的能力,这有两种方法可以做到,一种可能是:对象实现了Comparable接口,并提供了compareTo方法,另一种可能是:比较函数内嵌于对象的compare方法中,而该对象又实现了Comparator接口在Java.util中定义了Comparator
package com.hephec;
public interface Comparetor<AnyType>
{
/**
*Return the result of comparing lhs and rhs
*@param lhs first object
*@param rls second object
*return < 0 if lhs is less than rhs.
*return = 0 if lhs is equal to rhs.
*return >0 if lhs is more than rhs.
*throws ClassCaseException if object cannot be compared.
*/
int compare(AnyType lhs,AnyType rhs) throws ClassCastException;
}