?Collection?????????????Map(和Collection接口沒(méi)任何關(guān)系)
          ?????? / \???????????????????????? |
          ????? /?? \??????????????????????? |??????
          ?Set?? List??????????????SortedMap
          ??? /
          ?? /
          SortedSet
          (接口圖)

          所謂框架就是一個(gè)類(lèi)庫(kù)的集合。集合框架就是一個(gè)用來(lái)表示和操作集合的統(tǒng)一框架,包含了實(shí)現(xiàn)
          集合的接口和類(lèi)。
          ?
          ?集合框架中的接口

          ?.Collection: 集合層次中的根接口,JDK沒(méi)有提供這個(gè)接口直接的實(shí)現(xiàn)類(lèi)。
          ?.Set: 不能包含重復(fù)的元素。SortedSet是一個(gè)按照升序排列元素的Set。
          ?.List: 是一個(gè)有序的集合,可以包含重復(fù)的元素。提供了按照索引訪問(wèn)的方式。
          ?.Map: 包含了key-value對(duì)。Map不能包含重復(fù)的key。SortedMap是一個(gè)按照升序排列key的Map。
          ?
          ?集合框架中的實(shí)現(xiàn)類(lèi)
          ?
          ?實(shí)線表示繼承類(lèi),虛線表示實(shí)現(xiàn)類(lèi)。
          ?(圖如下)

          .ArrayList: 我們可以將其看做是能夠自動(dòng)增長(zhǎng)容量的數(shù)組。
          ?.利用ArrayList的toArray()返回一個(gè)數(shù)組。
          ?.Arrays.asList()返回一個(gè)列表。
          ?.迭代器(Iterator)給我們提供了一種通用的方式來(lái)訪問(wèn)集合中的元素。

          ?注意: 從集合類(lèi)中獲取一個(gè)數(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();? //將集合類(lèi)轉(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();
          ?? 如果要?jiǎng)h除元素,必須先調(diào)用next方法
          ? it.next();
          ? it.remove();
          ? while(it.hasNext())
          ? {
          ?? System.out.println(it.next());
          ? }
          ? */
          ? //迭代器的作用: 提供一組通用的訪問(wèn)方式
          ? 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類(lèi)

          ?.排序: Collections.sort(); [區(qū)別與Arrays.sort()]
          ? (1) 自然排序(natural ordering);
          ? (2) 實(shí)現(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();? //將集合類(lèi)轉(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();
          ?? 如果要?jiǎng)h除元素,必須先調(diào)用next方法
          ? it.next();
          ? it.remove();
          ? while(it.hasNext())
          ? {
          ?? System.out.println(it.next());
          ? }
          ? */
          ? //迭代器的作用: 提供一組通用的訪問(wèn)方式
          ? //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;
          ?//實(shí)現(xiàn)比較器,它總是和我們的一個(gè)類(lèi)相關(guān),用內(nèi)部類(lè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;
          ?}
          }

          主站蜘蛛池模板: 湘阴县| 延川县| 集贤县| 新巴尔虎右旗| 霍城县| 扶沟县| 新密市| 阜宁县| 锦州市| 阿合奇县| 米易县| 柳江县| 江西省| 滦南县| 舞钢市| 西藏| 西峡县| 通化市| 榕江县| 河北区| 全南县| 商南县| 宜丰县| 乐业县| 高雄市| 河北区| 大丰市| 平阳县| 茌平县| 罗江县| 庆城县| 馆陶县| 和政县| 渭源县| 石屏县| 玛曲县| 临湘市| 榆树市| 新河县| 驻马店市| 陈巴尔虎旗|