在之前的文章我們介紹了一下 Java 中的日期操作,本章我們來看一下 Java 集合框架中的Collection。
早在 Java 2 中之前,Java 就提供了特設類。比如:Dictionary, Vector, Stack, 和 Properties 這些類用來存儲和操作對象組。
雖然這些類都非常有用,但是它們缺少一個核心的,統一的主題。由於這個原因,使用 Vector 類的方式和使用 Properties 類的方式有着很大不同。
集合框架被設計成要滿足以下幾個目標。
-
該框架必須是高性能的。基本集合(動態數組,鏈表,樹,哈希表)的實現也必須是高效的。
-
該框架允許不同類型的集合,以類似的方式工作,具有高度的互操作性。
-
對一個集合的擴展和適應必須是簡單的。
為此,整個集合框架就圍繞一組標准接口而設計。你可以直接使用這些接口的標准實現,諸如: LinkedList, HashSet, 和 TreeSet 等,除此之外你也可以通過這些接口實現自己的集合。
從上面的集合框架圖可以看到,Java 集合框架主要包括兩種類型的容器,一種是集合(Collection),存儲一個元素集合,另一種是圖(Map),存儲鍵/值對映射。Collection 接口又有 3 種子類型,List、Set 和 Queue,再下面是一些抽象類,最后是具體實現類,常用的有 ArrayList、LinkedList、HashSet、LinkedHashSet、HashMap、LinkedHashMap 等等。
集合框架是一個用來代表和操縱集合的統一架構。所有的集合框架都包含如下內容:
-
接口:是代表集合的抽象數據類型。例如 Collection、List、Set、Map 等。之所以定義多個接口,是為了以不同的方式操作集合對象
-
實現(類):是集合接口的具體實現。從本質上講,它們是可重復使用的數據結構,例如:ArrayList、LinkedList、HashSet、HashMap。
-
算法:是實現集合接口的對象里的方法執行的一些有用的計算,例如:搜索和排序。這些算法被稱為多態,那是因為相同的方法可以在相似的接口上有着不同的實現。
除了集合,該框架也定義了幾個 Map 接口和類。Map 里存儲的是鍵/值對。盡管 Map 不是集合,但是它們完全整合在集合中。
集合框架體系如圖所示
聲明:以上內容節選自菜鳥教程,覺得總結的非常不錯就直接拿過來用了,通俗易懂。
接下來我們就用代碼來實現一下 Collection 的一些基本操作,在之前的文章我們想要創建一個數組之前必須先定義好類型和數組的長度,然后填充數組,如果數組長度要增加或減少時需要擴容或減容,代碼如下:
1 import java.util.Arrays; 2 3 public class Main { 4 public static void main(String[] args) { 5 int[] arr = new int[3]; 6 arr[0] = 1; 7 arr[1] = 2; 8 arr[2] = 3; 9 // int[] arr = {1, 2, 3}; // 這種方法也可以創建數組 10 for (int i = 0; i < arr.length; i++) { 11 // 輸出 arr1 數組值 12 System.out.println(arr[i]); // 1 2 3 13 } 14 // arr 源數組 arr.length+1 擴容數組長度 15 arr = Arrays.copyOf(arr, arr.length + 1); 16 for (int i : arr) { 17 System.out.println(i); // 1 2 3 0 18 } 19 } 20 }
從上面的代碼中可以看出,創建一個數組並且對數組進行操作還是很麻煩的,那我們通過 Collection 進行操作呢?如下:
1 import java.util.ArrayList; 2 import java.util.Collection; 3 4 /** 5 * java.util.Collection 包 6 * 集合,用於存儲一組元素,提供了維護集合的相關操作 7 * 其派生了兩個子接口: 8 * List:可重復集 9 * Set:不可重復集 10 * 元素是否重復是依靠元素資深 equals 方法比較的結果而定的 11 */ 12 public class Main { 13 public static void main(String[] args) { 14 /** 15 * boolean add(E e) 16 * 向集合中添加元素 17 * 當元素成功添加到集合后返回 true 18 */ 19 Collection collection = new ArrayList(); 20 collection.add(0); 21 // collection.add("one"); 可以添加成功,但不建議添加不同類型的數據,避免取出時造成麻煩 22 collection.add(1); 23 collection.add(2); 24 System.out.println(collection); // [0, 1, 2] 25 26 /** 27 * int size() 28 * 返回當前集合的元素個數 29 * */ 30 System.out.println(collection.size()); // 3 31 32 /** 33 * boolean isEmpty() 34 * 判斷當前集合是否含有任何元素 35 * 空集合 36 * */ 37 System.out.println(collection.isEmpty()); // false 38 39 /** 40 * boolean contains(E e) 41 * 判斷當前集合是否包含給頂元素 42 * */ 43 System.out.println(collection.contains(1)); // true 44 45 /** 46 * boolean remove(E e) 47 * 從集合中刪除指定元素,刪除成功返回 true 48 * 值刪除集合中第一個與給定元素 equals 比較為 true 的元素 49 * */ 50 collection.add(1); 51 System.out.println(collection.remove(1)); // true 52 System.out.println(collection); // [0, 2, 1] 53 System.out.println(collection.remove(1)); // true 54 System.out.println(collection); // [0, 2] 55 56 /** 57 * void clear() 58 * 清空集合 59 * */ 60 collection.clear(); 61 System.out.println(collection.size()); // 0 62 System.out.println(collection.isEmpty()); // true 63 } 64 }
在上面的代碼中,我們實現了 Collection 的一些基本用法,但是都是對 Collection 中的元素進行單體操作,當然 Collection 還有一些整體多元素操作,如下:
1 import java.util.ArrayList; 2 import java.util.Collection; 3 import java.util.HashSet; 4 5 /** 6 * java.util.Collection 包 7 * 集合,用於存儲一組元素,提供了維護集合的相關操作 8 * 其派生了兩個子接口: 9 * List:可重復集 10 * Set:不可重復集 11 * 元素是否重復是依靠元素資深 equals 方法比較的結果而定的 12 */ 13 public class Main { 14 public static void main(String[] args) { 15 Collection collection1 = new ArrayList(); 16 collection1.add("one"); 17 collection1.add("two"); 18 collection1.add("three"); 19 collection1.add("four"); 20 System.out.println(collection1); // [one, two, three, four] 21 22 Collection collection2 = new HashSet(); // set 集合內不能有重復元素,且為無序 23 collection2.add("one"); 24 collection2.add("two"); 25 collection2.add("three"); 26 System.out.println(collection2); // [one, two, three] 27 28 /** 29 * 取並集 30 * boolean addAll(Collection c) 31 * 將給定集合中的所有元素添加到當前集合中 32 * 添加后只要當前集合元素數量發生了變化,則返回 true 33 * */ 34 System.out.println(collection1.addAll(collection2)); // true 35 System.out.println(collection1); // [one, two, three, four, one, two, three] 36 System.out.println(collection2.addAll(collection1)); // true 37 System.out.println(collection2); // [four, one, two, three] 38 39 /** 40 * boolean containsAll(Collection c) 41 * 判斷當前集合是否包含給定集合中的所有元素 42 * */ 43 System.out.println(collection1.containsAll(collection2)); // true 44 45 /** 46 * boolean containsAll(Collection c) 47 * 從當前集合中刪除兩個集合中共有的元素 48 * */ 49 System.out.println(collection2.removeAll(collection1)); // true 50 System.out.println(collection2); // [] 51 } 52 }