標籤:
------Java培訓、Android培訓、iOS培訓、.Net培訓、期待與您交流! -------
一、基本概念
1)Collection:一個獨立元素的序列,這些元素都服從一條或多條規則。List必須按照插入的順序
儲存元素,而Set不能有重複元素。Queue按照排隊規則了確定對象產生的順序(通常與它們被插
入的順序相同)。
2)Map:一組成對的“索引值對”對象,允許你使用鍵來尋找值。ArrayList允許你使用數字來尋找值
,因此在某種意義上講,他講數字與對象關聯在了一起。映射表允許我們使用另一個對象來尋找某
個對象,它也被稱為“關聯陣列”,因為它將某些對象與另外一些對象關聯在了一起;或者被稱為“字
典”,因為你可以使用鍵對象來尋找值對象,就像在字典中使用單詞來定義一樣。Map是強大的編程
工具。
Collection vs Collections
首先,Collection 和 Collections 是兩個不同的概念。之所以放在一起,是為了更好的比較。
Collection是容器階層中根介面。而Collections是一個提供一些處理容器類靜態方法的類。
JDK不提供Collection介面的具體實現,而是提供了更加具體的子介面(如Set和List)實現。
那Collection介面存在有何作用呢?存在即是道理。
原因在於:所有容器的實作類別(如ArrayList實現了List介面,HashSet實現了Set介面)提供
了兩個‘標準’的建構函式來實現:1、一個無參的構造方法(void)2、一個帶有Collection類型單
參數構造方法,用於建立一個具有其參數相同元素新的Collection及其實作類別等。實際上:因為所
有通用的容器類遵從Collection介面,用第二種構造方法是允許容器之間相互的複製。
Conllection介面概括了序列的概念——一種存放一組對象的方式。下面用Integer對象填充一個
Collection(這裡用ArrayList表示)樣本,然後列印所產生的容器中的所有元素:
1 import java.util.*; 2 3 4 5 public class SimpleCollection { 6 7 public static void main(String[] args) { 8 9 Collection<Integer> c = new ArrayList<Integer>();10 11 for(int i = 0; i < 10; i++) {12 13 c.add(i); 14 15 }16 17 for(Integer i : c) {18 19 System.out.print(i + ", "); 20 21 }22 23 }24 25 }
因為這個樣本只使用了Collection方法,因此任何繼承自Collection的類的對象都可以正常
工作,但是ArrayList是最基本的序列類型。
add()方法的名稱就表明它是要將一個新元素放置到Collection中。但是,文檔中非常仔細
地敘述到:“要確保這個Collection包含指定的元素。”這個因為考慮到了Set的含義,因為在Set
中只有元素不存在的情況下才會添加。在使用ArrayList,或者任何種類的List時,add()總是表
示“把它放進去”,因為List不關心是否存在重複。
二、容器的列印
對TreeSet,HashSet,LinkedList,ArrayList,TreeMap,HashMap的例子如下:
1 import java.util.*; 2 3 public class CollectionAll 4 { 5 6 public static void main(String[] args) 7 { 8 printLists(); 9 10 printSets();11 12 printMaps();13 }14 15 private static void printLists()16 {17 List<String> a1 = new ArrayList<String>();18 a1.add("List");19 a1.add("Set");20 a1.add("Queue");21 a1.add("Map");22 a1.add("List");23 System.out.println("ArrayList Elements:");24 System.out.println(" " + a1);25 26 List<String> l1 = new LinkedList<String>();27 l1.add("List");28 l1.add("Set");29 l1.add("Queue");30 l1.add("Map");31 l1.add("List");32 System.out.println("LinkedList Elements:");33 System.out.println(" " + l1);34 }35 private static void printSets()36 {37 Set<String> h1 = new HashSet<String>();38 h1.add("List");39 h1.add("Set");40 h1.add("Queue");41 h1.add("Map");42 h1.add("List");43 System.out.println("HashSet Elements:");44 System.out.println(" " + h1);45 46 Set<String> t1 = new TreeSet<String>();47 t1.add("List");48 t1.add("Set");49 t1.add("Queue");50 t1.add("Map");51 t1.add("List");52 System.out.println("TreeSet Elements:");53 System.out.println(" " + t1);54 }55 56 private static void printMaps()57 {58 Map<String, String> h1 = new HashMap<String, String>();59 h1.put("List", "ArrayList");60 h1.put("Set", "HashSet");61 h1.put("Queue", "PriorityQueue");62 h1.put("Map", "HashMap");63 h1.put("List", "ArrayList");64 System.out.println("HashMap Elements:");65 System.out.println(" " + h1);66 67 Map<String, String> t1 = new TreeMap<String,String>();68 t1.put("List", "ArrayList");69 t1.put("Set", "HashSet");70 t1.put("Queue", "PriorityQueue");71 t1.put("Map", "HashMap");72 t1.put("List", "ArrayList");73 System.out.println("TreeMap Elements:");74 System.out.println(" " + t1);75 76 }77 }
運行結果:
ArrayList Elements: [List, Set, Queue, Map, List]LinkedList Elements: [List, Set, Queue, Map, List]HashSet Elements: [Map, Queue, Set, List]TreeSet Elements: [List, Map, Queue, Set]HashMap Elements: {Map=HashMap, Queue=PriorityQueue, Set=HashSet, List=ArrayList}TreeMap Elements: {List=ArrayList, Map=HashMap, Queue=PriorityQueue, Set=HashSet}
這裡展示了Java容器類庫中的兩種主要類型,它們的區別在於容器中每個“槽”儲存的元素個
數。Collection在每個槽中只能儲存一個元素。此類容器包含:
List:它以特定的順序儲存一組元素;
Set:元素不能重複;
Queue:值允許在容器的一“端”插入對象,並從另外一“端”移除對象。Map在每個槽內儲存了兩
個對象,即鍵和與之相關的值。
從列印結果來看,ArrayList和LinkedList都是List類型,它們都按照被插入的順序儲存元素
。兩者的不同之處不僅在於執行某些類型的操作時的效能,而且LinkedList包含的操作也多於
ArrayList。
HashSet、TreeSet和LinkedHashSet都是Set類型,每個相同的項只有儲存一次,但輸出
也顯示了不同的Set實現儲存元素的方式也不同。HashSet使用的是相當複雜的方式來儲存元素
的,因此,儲存的順序看起來並無實際意義。如果儲存順序很重要,那麼可以使用TreeSet,它
按照比較結果的升序儲存對象;或者使用LinkedHashSet,它按照被添加的順序儲存對象。
本例使用了三種基本風格的Map:HashMap、TreeMap和LinkedHashSet。與HashSet一
樣,HashMap也提供了最快的尋找技術,也沒有按照任何明顯的順序來曹操其元素。TreeMap
按照比較結果的升序儲存鍵,而LinkedHashMap則按照插入順序儲存鍵,同時還保留了HashMap
的查詢速度。
Java——(二)泛型與容器