1.Collection
原文链接:http://pengcqu.iteye.com/blog/492196
java.util.Collection 是一个集合接口。它提供了对集合对象进行基本操作的通用接口方法。Collection接口在Java 类库中有很多具体的实现。Collection接口的意义是为各种具体的集合提供了最大化的统一操作方式。
Collection
├List
│├LinkedList
│├ArrayList
│└Vector
│ └Stack
└Set
2.Collections
原文链接:http://pengcqu.iteye.com/blog/492196
java.util.Collections 是一个包装类。它包含有各种有关集合操作的静态多态方法。此类不能实例化,就像一个工具类,服务于Java的Collection框架。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestCollections {
public static void main(String args[]) {
//注意List是实现Collection接口的
List list = new ArrayList();
double array[] = { 112, 111, 23, 456, 231 };
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections.sort(list);
for (int i = 0; i < array.length; i++) {
System.out.println(list.get(i));
}
// 结果:23.0 111.0 112.0 231.0 456.0
}
}
3.Collections中的Collections.sort()函数的用法
有两种使用方法,一是list中的对象实现Comparable接口;二是list中的对象实现一个内部类,实现Comparable接口。
参考链接1:http://www.cnblogs.com/wanqieddy/archive/2011/06/20/2085057.html
参考链接2:http://viver120.blog.163.com/blog/static/60072482013010111228695/
第一种是list中的对象实现Comparable接口,如下:
函数原型:sort(List list)
说明:参数是要参与排序列表的List对象
实例说明:参与排序的列表的元素Student必须实现Comparable接口的
public int compareTo(Object o) 方法,在里面写对比的原则。然后调用Colletions.sort(排序对象的列表)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
class ArrayListTest
{
public static void printElements(Collection c){
Iterator it = c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static void main(String[] args){
ArrayList<Student> al = new ArrayList<Student>();
al.add(new Student(2,"aora"));
al.add(new Student(1,"longyu"));
al.add(new Student(3,"goso"));
Collections.sort(al);
printElements(al);
}
}
class Student implements Comparable
{
int num;
String name;
Student(int num,String name){
this.num = num;
this.name = name;
}
public int compareTo(Object o){
Student s = (Student)o;
return num > s.num ? 1 : (num == s.num ? 0 : -1);
}
public String toString(){
return "num = " + this.num + ",name = " + this.name;
}
}
第二种是实现比较器(Comparator)接口。
函数原型:sort(List<T> list, Comparator<? super T> c)
说明:第一个参数同左,第二个参数是构建对比规则的对比器Comparator。
实例说明(如下):在参与排序的列表的元素Student中写一个内部类作为Student的对比器,这个对比器要实现Comparator接口的 public int compare(Object o1,Object o2)方法,然后调用Colletions.sort(排序对象的列表,对比器)
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Comparator;
class ArrayListTest
{
public static void printElements(Collection c){
Iterator it = c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static void main(String[] args){
ArrayList<Student> al = new ArrayList<Student>();
al.add(new Student(2,"qingan"));
al.add(new Student(1,"longyu"));
al.add(new Student(3,"goso"));
al.add(new Student(2,"aora"));
Collections.sort(al,new Student.studentComparator());
printElements(al);
}
}
class Student
{
int num;
String name;
Student(int num,String name)
{
this.num = num;
this.name = name;
}
//静态内部类
static class studentComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int result = s1.num > s2.num ? 1 : (s1.num == s2.num ? 0 : -1);// 注意:此处在对比num相同时,再按照name的首字母比较。
if(result == 0){
result = s1.name.compareTo(s2.name);
}
return result;
}
} //静态内部类结束
public String toString(){
return "num = " + this.num + ",name = " + this.name;
}
}