???? Collection?????????????????? Map(和Collection接口沒任何關(guān)系)
?????? / \???????????????????????? |
????? /?? \??????????????????????? |?????
??? Set?? List????????????????? SortedMap
??? /
?? /
?SortedSet
(接口圖)
所謂框架就是一個類庫的集合。集合框架就是一個用來表示和操作集合的統(tǒng)一框架,包含了實現(xiàn)
集合的接口和類。
?
?集合框架中的接口
?.Collection: 集合層次中的根接口,JDK沒有提供這個接口直接的實現(xiàn)類。
?.Set: 不能包含重復(fù)的元素。SortedSet是一個按照升序排列元素的Set。
?.List: 是一個有序的集合,可以包含重復(fù)的元素。提供了按照索引訪問的方式。
?.Map: 包含了key-value對。Map不能包含重復(fù)的key。SortedMap是一個按照升序排列key的Map。
?
?集合框架中的實現(xiàn)類
?
?實線表示繼承類,虛線表示實現(xiàn)類。
?(圖如下)

?.ArrayList: 我們可以將其看做是能夠自動增長容量的數(shù)組。
?.利用ArrayList的toArray()返回一個數(shù)組。
?.Arrays.asList()返回一個列表。
?.迭代器(Iterator)給我們提供了一種通用的方式來訪問集合中的元素。
?注意: 從集合類中獲取一個數(shù)組 toArray(),從數(shù)組獲取列表利用Arrays.asList()
?例子:
import java.util.*;
class ArrayListTest
{
?public static void printElement(Collection c)
?{
??Iterator it = c.iterator();
??while(it.hasNext())
??{
???System.out.println(it.next());
??}
?}
?public static void main(String[] args)
?{
??ArrayList a = new ArrayList();
??/*
??a.add("abc");
??a.add("def");
??a.add("hjk");
??*/
??
??a.add(new Point(1,1));
??a.add(new Point(2,2));
??a.add(new Point(3,3));
??/*
??Object[] o;
??o=a.toArray();? //將集合類轉(zhuǎn)換為數(shù)組
??for(int i=0;i<o.length;i++)
??{
???System.out.println(o[i]);
??}
??
??List b = new ArrayList();
??b=Arrays.asList(o);
??System.out.println(b);
??
??for(int i=0;i<a.size();i++)
??{
???System.out.println(a.get(i));
??}
??
??System.out.println(a);
??System.out.println(a);
??*/
??
??/*
??Iterator it = a.iterator();
?? 如果要刪除元素,必須先調(diào)用next方法
??it.next();
??it.remove();
??while(it.hasNext())
??{
???System.out.println(it.next());
??}
??*/
??//迭代器的作用: 提供一組通用的訪問方式
??printElement(a);
?}
}
class Point
{
?int x, y;
?Point(int x, int y)
?{
??this.x=x;
??this.y=y;
?}
?public String toString()
?{
??return "x="+x+","+"y="+y;
?}
}
?Collections類
?.排序: Collections.sort(); [區(qū)別與Arrays.sort()]
? (1) 自然排序(natural ordering);
? (2) 實現(xiàn)比較器(Comparator)接口。
?.取最大和最小的元素: Collections.max(),Collections.min();
?.在已排序的List中搜索指定的元素: Collections.binarySearch()。
?代碼示例:
import java.util.*;
class ArrayListTest
{
?public static void printElement(Collection c)
?{
??Iterator it = c.iterator();
??while(it.hasNext())
??{
???System.out.println(it.next());
??}
?}
?public static void main(String[] args)
?{
??/*
??ArrayList a = new ArrayList();
??
??a.add("abc");
??a.add("def");
??a.add("hjk");
??
??
??a.add(new Point(1,1));
??a.add(new Point(2,2));
??a.add(new Point(3,3));
??
??Object[] o;
??o=a.toArray();? //將集合類轉(zhuǎn)換為數(shù)組
??for(int i=0;i<o.length;i++)
??{
???System.out.println(o[i]);
??}
??
??List b = new ArrayList();
??b=Arrays.asList(o);
??System.out.println(b);
??
??for(int i=0;i<a.size();i++)
??{
???System.out.println(a.get(i));
??}
??
??System.out.println(a);
??System.out.println(a);
??*/
??
??/*
??Iterator it = a.iterator();
?? 如果要刪除元素,必須先調(diào)用next方法
??it.next();
??it.remove();
??while(it.hasNext())
??{
???System.out.println(it.next());
??}
??*/
??//迭代器的作用: 提供一組通用的訪問方式
??//printElement(a);
??
??Student s1 = new Student(1,"zhangsan");
??Student s2 = new Student(2,"lisi");
??Student s3 = new Student(3,"wangwu");
??Student s4 = new Student(3,"blovesaga");
??
??ArrayList a = new ArrayList();
??a.add(s1);
??a.add(s2);
??a.add(s3);
??a.add(s4);
??
??//Collections.sort(a);
??Collections.sort(a,new Student.StudentComparator());
??printElement(a);
?}
}
class Point
{
?int x, y;
?Point(int x, int y)
?{
??this.x=x;
??this.y=y;
?}
?public String toString()
?{
??return "x="+x+","+"y="+y;
?}
}
class Student implements Comparable
{
?int num;
?String name;
?//實現(xiàn)比較器,它總是和我們的一個類相關(guān),用內(nèi)部類
?static class StudentComparator implements Comparator? //為了調(diào)用方便聲明為靜態(tài)的
?{
??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);
???if(result==0)
???{
????return s1.name.compareTo(s2.name);
???}
???return result;
??}
?}
?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+":"+name;
?}
}
?
?