天天看點

黑馬程式員 JAVA基礎<四> 集合

                    ------- android教育訓練、java教育訓練、期待與您交流! ----------

1 Collection(add,remove,contains,size,iterator)

 |--List(有序,可重複 add(index),get(index),set(index),remove(index))

         |--ArrayList

                底層資料結構是數組,查詢快,增删慢。

                線程不安全,效率高。

         |--Vector

               底層資料結構是數組,查詢快,增删慢。

               線程安全,效率低。

         |--LinkedList

               底層資料結構是連結清單,查詢慢,增删快。

                線程不安全,效率高。

|--Set(無序,唯一)

            |--HashSet

                  底層資料結構是哈希表。

                  如何保證元素唯一性呢?

                 它依賴兩個方法,hashCode()和equals()方法。

                  首先根據hashCode()進行比較:

                  相同:繼續,equals()方法

                 如果傳回true,不添加

                    如果傳回false,添加

               不相同:直接添加到集合中。

           |--LinkedHashSet

                 底層資料結構是哈希表和連結清單。

                哈希表保證元素唯一。

                  連結清單保證元素有序。

           |--TreeSet

                底層資料結構是二叉樹。

                如何保證元素唯一性呢?

                根據自然排序或比較器傳回的最終結果是不是0來确定元素是否重複。

                 如果是0,不添加。否則,添加。

                 怎麼實作排序的呢?

            方式1:讓元素本身具備比較性

              實作Comparable接口

            方式2:讓集合具備比較性

             實作Comparator接口

             比較接口:

             Comparable:自然排序比較,被元素本身實作。

             Comparator:比較器接口,用于集合實作。

2:集合的使用

集合的使用步驟:

        A:建立集合對象

        B:建立元素對象

        C:把元素對象添加到集合對象中

        D:周遊集合

       周遊的方式:

           Collection:

                      疊代器:

                      增強for:

         |--List

           |--get()和size()結合

          |--Set

List:如果需要索引,那麼用get()方式。否則,就用Collection的方式。

為了友善我們一般使用for。

3:什麼時候使用哪種集合:

元素是否唯一?

          是:Set

元素需要排序嗎?

           需要:TreeSet

          不需要:HashSet

如果不知道,用HashSet

           不是:List

線程是否安全:

         安全:Vector

         不安全:ArrayList,LinkedList

查詢多:ArrayList

增删多:LinkedList

       如果不知道,用ArrayList。

        如果不知道,用ArrayList。

4:集合中用到的資料結構總結

        ArrayXxx:底層資料結構是數組,查詢快,增删慢

        LinkXxx:底層資料結構是連結清單,查詢慢,增删快

        HashXxx:底層資料結構是哈希表,跟hashCoe()和equals()有關。

         TreeXxx:底層資料結構是二叉樹,跟Comparable和Comparator有關。

代碼一:用疊代器周遊數組

   Iterator it = array.iterator();

  while (it.hasNext()) {

   Student s = (Student) it.next();

   System.out.println(s.getName() + "***" + s.getAge());

  }

  代碼二:HashSet保持唯一性要重寫HashCode()和equals()方法

例子:

public int hashCode() {

  return this.name.hashCode()+this.age*17;

 }

 @Override

 public boolean equals(Object obj) {

  if (this == obj) {

   return true;

  }

  if (!(obj instanceof Student)) {

   return false;

  }

  Student s = (Student) obj;

  return this.name.equals(s.name) && this.age == s.age;

 }

代碼三:TreeSet 保持唯一性要實作comparable 或者comparetor 例子:

public static void main(String[] args) {

  TreeSet<Student> tree = new TreeSet<Student>(new Comparator<Student>(){

   @Override

   public int compare(Student o1, Student o2) {

    // TODO Auto-generated method stub

    int num = o1.getName().length()-o2.getName().length();

    int num2 =(num ==0)?o1.getAge()-o2.getAge():num;

    int num3 = (num2==0)?o1.getName().compareTo(o2.getName()):num2;

    return num3;

   }

  });

           ------- android教育訓練、java教育訓練、期待與您交流! ----------