Collection是一个集合接口,它提供了对集合对象进行基本操作的通用接口方法。实现该接口类的主要有List和Set,该接口的设计目标是为各种具体的集合提供最大化的统一操作方式。
Collections是对集合类的一个包装类,它提供了一系列静态方法实现对各种集合的搜索、排序以及线程安全化等操作,其中大多数方法都是用于处理线性表。Collections类不能实例化,如同一个工具类,服务于Collection框架。如果在使用Collections类的方法时,对应的Collection的对象null,则这些方法都会抛出NullPointerException。
下面为Collections使用的例子:
import java.util.*
public class Test{
public static void main(String args[]){
List<Integer>list=new LinkedList<Integer>();
int array[]={1,7,3,2};
for(int i=0;i<array.length;i++){
list.add(new Integer(array[i]));
}
Collections.sort(list);
for(int i=0;i<array.length;i++){
system.out.println(list.get(i));
}
}
}
程序运行的结果为:
1 2 3 7