黑馬程序員 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培訓期待與您交流! ----------

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章