Java筆記--集合

來源:互聯網
上載者:User

標籤:

1、Java集合類可以用於儲存數量不等的多個對象,還可以用於儲存具有映射關係的關聯陣列。

2、Java集合可分為Collection和Map兩種體系:

--Collection:1)Set:元素無序、不可重複的集合;2)List:元素有序,可重複的集合

--Map:具有映射關係"key/value對"的集合。

3、Collection介面:

  |----List介面:

    |----ArrayList、LinkedList、Vector

  |----Set介面:

    |----HashSet、LinkedHashSet、TreeSet

   Map介面:

  |----HashMap、LinkedHashMap、TreeMap、HashTable(子類:Properties)

4、Collection介面中定義的常用方法:

--add(Object o);//添加對象;

--addAll(Collection col);//將集合col裡面的元素全部添加到當前集合;

--clear();//清空當前集合;

--contains(Object o);//判斷當前結合是否包含對象o;

--containsAll(Collection col);//判斷當前集合是否包含集合col中的所有對象; 

--isEmpty();//判斷當前集合是否為空白;

--remove(Object o);//移除對象o

--removeAll(Collection col);//移除當前集合與col公有的對象(求差集);

--size();//擷取當前集合長度(元素個數);

--retainAll(Collection col);//求當前集合與col的公有的元素,並返回給當前集合(求交集);

--equals(Collection col);//判斷當前集合與集合col中的對象是否全部相等;

--iterator();//返回當前集合迭代器;

5、List中相對於Collection新增的方法:

void add(int index, Object o);//在制定的index位置處添加元素

boolean addAll(int index, Collection col);//在指定的index位置處添加col中的元素

Object get(int index);//擷取指定index的元素

int indexOf(Object o);//返回元素o在集合中首次出現的位置,若不存在o則返回-1

int lastIndexOf(Object o);//返回元素o在集合中最後次出現的位置,若不存在o則返回-1

Object remove(int index);//刪除指定index的元素

Object set(int index, Object o);//設定指定index的元素為o

List subList(int fromIndex, int toIndex);//返回當前集合從fromIndex到toIndex的子集合

6、HashSet添加自訂類型時,要實現其不可重複性,則必須要重寫equals()和hashCode()兩個方法,唯寫equals()無法保證。

--當向Set中添加對象時,首先調用此對象所在類的hashCode()方法,計算此對象的雜湊值,此雜湊值決定了此對象在Set中的儲存位置。若此位置之前沒有Object Storage Service,則這個對象直接儲存到此位置,若此位置已有Object Storage Service,再通過equals()比較這兩個對象是否相同。如果相同,後一個對象無法添加。

註:若雜湊值一樣,但是equals()為false,則兩個對象存於同一位置,通常不推薦這麼使用,需重新設計hashCode()方法。

7、LinkedHashSet:使用鏈表維護了添加順序,當遍曆集合時,是按照添加的順序來遍曆的(效能略低於HashSet)。

8、TreeSet:1)添加的元素必須是同一類型的;

      2)可以按照添加進集合的元素的指定順序遍曆,例如int是從小到大;

      3)有兩種排序:自然排序,定製排序

      4)自然排序需實現Comparable介面,定製排序需要實現Comparator介面。

      5)向TreeSet中添加元素時,首先按照compareTo()進行比較,一旦返回0,程式將認為添加的元素與已有元素相同,因而無法添加。

9、Map介面方法:

Object put(Object key, Object value);

Object remove(Object key);

void putAll(Map map);

void clear();

Object get(Object key);

boolean containsKey(Object key);

boolean containValue(Object value);

int size();

boolean isEmpty();

boolean equals();

10、Map遍曆:

1)遍曆key集  

Set set = map.keySet();Iterator it = set.iterator();while(it.hasNext()){    System.out.println(it.next());}

2)遍曆valus集

Collection coll = map.values();it = coll.iterator();while(it.hasNext()){    System.out.println(it.next());}

3)遍曆key-value(entry)集

方式一,通過遍曆keySet來得到value

Set set = map.keySet();Iterator it = set.iterator();while(it.hasNext()){    Object key = it.next();    Object value = map.get(key);    System.out.println(key + "--->" + value);}

方式二,直接遍曆entry集

Set set1 = map.entrySet();for(Object obj : set1){    Map.Entry entry = (Map.Entry)obj;    System.out.println(entry.getKey() + "--->" + entry.getValue());}

 11、TreeMap:按照添加進Map中的元素的key的指定屬性進行排序。key必須是同一類型的。

12、Hashtable是個Map實作類別,安全執行緒,與HashMap不同,它不允許使用null作為key和value,很少使用。

13、Properties:常用來處理屬性檔案,鍵和值都為String類型的。

public static void main(String[] args) throws FileNotFoundException, IOException {    Properties pros = new Properties();    pros.load(new FileInputStream(new File("jdbc.properties")));            String user = pros.getProperty("user");    System.out.println(user);            String psw = pros.getProperty("psw");    System.out.println(psw);}

 14、Collections是一個操作Set、List和Map等集合的工具類,提供了一系列靜態(static)的方法對集合元素進行排序、查詢和修改等操作,還提供了對集合對象設定不可變、對集合對象實現同步控制方法。

常用方法:1)reverse(List);//反轉List中的元素

     2)shuffle(List);//對List集合元素進行隨機排序

 

 

     3)sort(List);//很據元素的自然排序對List進行排序

     4)sort(List, Comparator);//根據定製排序對List進行排序

     5)swap(List, int, int);//將指定List中的兩個元素交換

     6)Object max(Collection);//根據元素的自然順序,返回最大值

     7)Object max(Collection, Comparator);

     8)Object min(Collection);//根據元素的自然順序,返回最小值

     9)Object min(Collection, Comparator);

     10)int frequency(Collection, Object);//返回指定對象在集合中出現的次數

     11)void copy(List dest, List src);//將src中的元素複製到dest中

     12)boolean replaceAll(List list, Object oldVal, Object newVal);//將指定List中的oldVal替換為newVal

 

Java筆記--集合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.