學習java的同學注意了?。?! 學習過程中遇到什么問題或者想獲取學習資源的話,歡迎加入Java學習交流群,群號碼:523047986 我們一起學Java!
在這篇文章里,我們關注Java中的集合(Collection)。集合是編程語言中基礎的一部分,Java自JDK早期,就引入了Java Collection Framework。設計JCF的那個人,后來還寫了一本書,叫《Effective Java》。
Java中的集合主要集中在2部分,一部分是java.util包中,一部分是java.util.concurrent中,后者是在前者的基礎上,定義了一些實現了同步功能的集合。
這篇文章主要關注java.util下的各種集合對象。Java中的集合對象可以粗略的分為3類:List、Set和Map。對應的UML圖如下(包括了java.util下大部分的集合對象):
?。ㄟ@張圖經過縮放已經變形,完整清晰版圖片請參見:http://files.cnblogs.com/wing011203/java_collection_structure.z
1 PRivate static Collection initCollection() 2 { 3 Collection<Integer> collection = new ArrayList<Integer>(); 4 Random r = new Random(); 5 for (int i = 0 ; i < 5; i++) 6 { 7 collection.add(new Integer(r.nextInt(100))); 8 } 9 10 return collection;11 }CollectionByIterator(Collection<Integer> collection)2 {3 Iterator<Integer> iterator = collection.iterator();4 System.out.println("The value in the list:");5 while(iterator.hasNext())6 {7 System.out.println(iterator.next());8 }9 }在對集合進行操作的過程中,遍歷是一個經常使用的操作,我們可以使用兩種方式對集合進行遍歷:
1) 使用迭代器對集合進行遍歷。正如上面描述Collection接口時所說,所有集合都會有一個迭代器,我們可以用它來遍歷集合。
1 private static void access2)使用foreach遍歷集合。
1 private static void accessCollectionByFor(Collection<Integer> collection)2 {3 System.out.println("The value in the list:");4 for(Integer value : collection)5 {6 System.out.println(value);7 }8 }List
Java中的List是對數組的有效擴展,它是這樣一種結構,如果不使用泛型,它可以容納任何類型的元素,如果使用泛型,那么它只能容納泛型指定的類型的元素。和數組相比,List的容量是可以動態擴展的。
List中的元素是可以重復的,里面的元素是“有序”的,這里的“有序”,并不是排序的意思,而是說我們可以對某個元素在集合中的位置進行指定。
List中常用的集合對象包括:ArrayList、Vector和LinkedList,其中前兩者是基于數組來進行存儲,后者是基于鏈表進行存儲。其中Vector是線程安全的,其余兩個不是線程安全的。
List中是可以包括null的,即使是使用了泛型。
ArrayList可能是我們平時用到的最多的集合對象了,在上述的示例代碼中,我們也是使用它來實例化一個Collection對象,在此不再贅述。
Vector
Vector的示例如下,首先我們看如何生成和輸出Vector:
1 private static void vectorTest1() 2 { 3 List<Integer> list = new Vector<Integer>(); 4 for (int i = 0 ; i < 5; i++) 5 { 6 list.add(new Integer(100)); 7 } 8 list.add(null); 9 System.out.println("size of vector is " + list.size());10 System.out.println(list);11 }它的元素中,既包括了重復元素,也包括了null,輸出結果如下:
size of vector is 6[100, 100, 100, 100, 100, null]下面的示例,演示了Vector中的一些常用方法:
1 private static void vectorTest2() 2 { 3 Vector<Integer> list = new Vector<Integer>(); 4 Random r = new Random(); 5 for (int i = 0 ; i < 10; i++) 6 { 7 list.add(new Integer(r.nextInt(100))); 8 } 9 System.out.println("size of vector is " + list.size());10 System.out.println(list);11 System.out.println(list.firstElement());12 System.out.println(list.lastElement());13 System.out.println(list.subList(3, 8));14 List<Integer> temp = new ArrayList<Integer>();15 for(int i = 4; i < 7; i++)16 {17 temp.add(list.get(i));18 }19 list.retainAll(temp);20 System.out.println("size of vector is " + list.size());21 System.out.println(list);22 }它的輸出結果如下:
size of vector is 10[39, 41, 20, 9, 29, 32, 54, 12, 94, 82]3982[9, 29, 32, 54, 12]size of vector is 3[29, 32, 54]LinkedList
LinkedList使用鏈表來存儲數據,它的示例代碼如下:
LinkedList示例
這里列出了LinkedList常用的各個方法,從方法名可以看出,LinkedList也可以用來實現棧和隊列。
輸出結果如下:
size of linked list is 11[17, 21, 5, 84, 19, 57, 68, 26, 27, 47, null]1717null1717null1721null5size of linked list is 8[100, 84, 19, 57, 68, 26, 27, 47]Set
Set 和List類似,都是用來存儲單個元素,單個元素的數量不確定。但Set不能包含重復元素,如果向Set中插入兩個相同元素,那么后一個元素不會被插入。
Set可以大致分為兩類:不排序Set和排序Set,不排序Set包括HashSet和LinkedHashSet,排序Set主要指TreeSet。其中HashSet和LinkedHashSet可以包含null。
HashSet
HashSet是由Hash表支持的一種集合,它不是線程安全的。
我們來看下面的示例,它和Vector的第一個示例基本上是相同的:
1 private static void hashSetTest1() 2 { 3 Set<Integer> set = new HashSet<Integer>(); 4 5 for (int i = 0; i < 3; i++) 6 { 7 set.add(new Integer(100)); 8 } 9 set.add(null);10 11 System.out.println("size of set is " + set.size());12 System.out.println(set);13 }這里,HashSet中沒有包含重復元素,但包含了null,和Vector不同,這里的輸出結果如下:
size of set is 2[null, 100]對于HashSet是如何判斷兩個元素是否是重復的,我們可以深入考察一下。Object中也定義了equals方法,對于HashSet中的元素,它是根據equals方法來判斷元素是否相等的,為了證明這一點,我們可以定義個“不正?!钡念愋停?/p>
1 class MyInteger 2 { 3 private Integer value; 4 5 public MyInteger(Integer value) 6 { 7 this.value = value; 8 } 9 10 public String toString()11 {12 return String.valueOf(value);13 }14 15 public int hashCode()16 {17 return 1;18 }19 20 public boolean equals(Object obj)21 {22 return false;23 }24 }可以看到,對于MyInteger來說,對于任意兩個實例,我們都認為它是不相等的。
下面是對應的測試方法:
1 private static void hashSetTest2() 2 { 3 Set<MyInteger> set = new HashSet<MyInteger>(); 4 5 for (int i = 0; i < 3; i++) 6 { 7 set.add(new MyInteger(100)); 8 } 9 10 System.out.println("size of set is " + set.size());11 System.out.println(set);12 }它的輸出結果如下:
size of set is 3[100, 100, 100]可以看到,現在HashSet里有“重復”元素了,但對于MyInteger來說,它們不是“相同”的。
TreeSet
TreeSet是支持排序的一種Set,它的父接口是SortedSet。
我們首先來看一下TreeSet都有哪些基本操作:
1 private static void treeSetTest1() 2 { 3 TreeSet<Integer> set = new TreeSet<Integer>(); 4 5 Random r = new Random(); 6 for (int i = 0 ; i < 5; i++) 7 { 8 set.add(new Integer(r.nextInt(100))); 9 }10 11 System.out.println(set);12 System.out.println(set.first());13 System.out.println(set.last());14 System.out.println(set.descendingSet());15 System.out.println(set.headSet(new Integer(50)));16 System.out.println(set.tailSet(new Integer(50)));17 System.out.println(set.subSet(30, 60));18 System.out.println(set.floor(50));19 System.out.println(set.ceiling(50));20 }它的輸出結果如下:
[8, 42, 48, 49, 53]853[53, 49, 48, 42, 8][8, 42, 48, 49][53][42, 48, 49, 53]4953TreeSet中的元素,一般都實現了Comparable接口,默認情況下,對于Integer來說,SortedList是采用升序來存儲的,我們也可以自定義Compare方式,例如以降序的方式來存儲。
下面,我們首先重新定義Integer:
定義MyInteger2對象
下面是測試代碼:
1 private static void treeSetTest2() 2 { 3 TreeSet<Integer> set1 = new TreeSet<Integer>(); 4 TreeSet<MyInteger2> set2 = new TreeSet<MyInteger2>(); 5 Random r = new Random(); 6 for (int i = 0 ; i < 5; i++) 7 { 8 int value = r.nextInt(100); 9 set1.add(new Integer(value));10 set2.add(new MyInteger2(value));11 }12 System.out.println("Set1 as below:");13 System.out.println(set1);14 System.out.println("Set2 as below:");15 System.out.println(set2);16 }代碼的運行結果如我們所預期的那樣,如下所示:
Set1 as below:[13, 41, 42, 45, 61]Set2 as below:[61, 45, 42, 41, 13]Map
Map中存儲的是“鍵值對”,和Set類似,Java中的Map也有兩種:排序的和不排序的,不排序的包括HashMap、Hashtable和LinkedHashMap,排序的包括TreeMap。
非排序Map
HashMap和Hashtable都是采取Hash表的方式進行存儲,HashMap不是線程安全的,Hashtable是線程安全的,我們可以把HashMap看做是“簡化”版的Hashtable。
HashMap是可以存儲null的,無論是對Key還是對Value。Hashtable是不可以存儲null的。
無論HashMap還是Hashtable,我們觀察它的構造函數,就會發現它可以有兩個參數:initialCapacity和loadFactor,默認情況下,initialCapacity等于16,loadFactor等于0.75。這和Hash表中可以存放的元素數目有關系,當元素數目超過initialCapacity*loadFactor時,會觸發rehash方法,對hash表進行擴容。如果我們需要向其中插入過多元素,需要適當調整這兩個參數。
我們首先來看HashMap的示例:
1 private static void hashMapTest1() 2 { 3 Map<Integer,String> map = new HashMap<Integer, String>(); 4 5 map.put(new Integer(1), "a"); 6 map.put(new Integer(2), "b"); 7 map.put(new Integer(3), "c"); 8 9 System.out.println(map);10 System.out.println(map.entrySet());11 System.out.println(map.keySet());12 System.out.println(map.values());13 }這會輸出HashMap里的元素信息,如下所示。
{1=a, 2=b, 3=c}[1=a, 2=b, 3=c][1, 2, 3][a, b, c]下面的示例是對null的演示:
1 private static void hashMapTest2() 2 { 3 Map<Integer,String> map = new HashMap<Integer, String>(); 4 5 map.put(null, null); 6 map.put(null, null); 7 map.put(new Integer(4), null); 8 map.put(new Integer(5), null); 9 10 System.out.println(map);11 System.out.println(map.entrySet());12 System.out.println(map.keySet());13 System.out.println(map.values());14 }執行結果如下:
{null=null, 4=null, 5=null}[null=null, 4=null, 5=null][null, 4, 5][null, null, null]接下來我們演示Hashtable,和上述兩個示例基本上完全一樣(代碼不再展開):
Hashtable示例
執行結果如下:
{3=c, 2=b, 1=a}[3=c, 2=b, 1=a][3, 2, 1][c, b, a]Exception in thread "main" java.lang.NullPointerException at java.util.Hashtable.put(Unknown Source) at sample.collections.MapSample.hashTableTest2(MapSample.java:61) at sample.collections.MapSample.main(MapSample.java:11)可以很清楚的看到,當我們試圖將null插入到hashtable中時,報出了空指針異常。
排序Map
排序Map主要是指TreeMap,它對元素增、刪、查操作時的時間復雜度都是O(log(n))。它不是線程安全的。
它的特點和TreeSet非常像,這里不再贅述。
學習Java的同學注意了!??! 學習過程中遇到什么問題或者想獲取學習資源的話,歡迎加入Java學習交流群,群號碼:523047986 我們一起學Java!
新聞熱點
疑難解答