集合綜述
1.Collection接口
Java集合類中最基礎(chǔ)的接口是Collection,它定義了兩個基本方法:
Public interface Collection<E>{
boolean add(E element);
Iterator<E> iterator();
}
add是向Collection中添加一個元素,當(dāng)添加的內(nèi)容確實(shí)對Collection發(fā)生了有效變更的話add方法返回真,否則返回假,比如你向一個Set添加重復(fù)的元素時,Set內(nèi)容不會變化,add方法會返回假。
iterator方法返回實(shí)現(xiàn)了Iterator接口的對象,你可以借此對Collection中的內(nèi)容進(jìn)行挨個遍歷。
2.Iterator接口
Iterator有三個方法:
Public interface Iterator<E>{
E next();
boolean hasNext();
void remove();
}
通過重復(fù)調(diào)用next方法,你可以挨個遍歷Collection中的元素。然而,當(dāng)遍歷到Collection末端時,此方法會拋出一個NoSuchElementException異常,因此在遍歷時你就需要hasNext方法和next方法配合使用。 hasNext方法在當(dāng)前遍歷位置后仍有元素時會返回真。配合例子如下:
Collection<String> c=….;
Iterator<String> iterator=c.iterator();
While(iterator.hasNext()){
String str=iterator.next();
…….
}
remove方法能刪除上一次調(diào)用next時指向的元素,注意不能連續(xù)兩次調(diào)用remove方法,否則會拋出IllegalStateException.
3.Collection接口的其它方法
int size():返回當(dāng)前存儲的元素個數(shù)
boolean isEmpty():當(dāng)集合中沒有元素時返回真
boolean contains(Object obj):當(dāng)集合中有元素和obj值相等時返回真.
boolean containsAll(Collection<?> other):當(dāng)集合包含另一集合的全部元素時返回真.
bBoolean addAll (Collection<? extends E> other):把其它集合中的元素添加到集合中來,當(dāng)此集合確實(shí)發(fā)生變化時返回真.
boolean remove(Object obj):刪除一個和obj值相等的元素,當(dāng)吻合的元素被刪除時返回真.
boolean removeAll(Collection<?> other) :從本集合中刪除和另一集合中相等的元素,當(dāng)本集合確實(shí)發(fā)生變化時返回真.(差集)
void clear():清除集合中的所有元素
boolean retainAll(Collection<?> other) :從本集合中刪除和另一集合中不相等的元素,當(dāng)本集合確實(shí)發(fā)生變化時返回真.(交集)
Object[] toArray()
返回由集合中元素組成的數(shù)組
4.實(shí)現(xiàn)了Collection接口的具體類
ArrayList:帶下標(biāo)的隊列,增加和收縮時都是動態(tài)的.
LinkedList:有序隊列,在任意位置插入和刪除都是高效的.
HashSet:不存在重復(fù)的非排序集合.
TreeSet:自動排序的不存在重復(fù)的集合.
LinkedHashSet:保持了插入時順序的哈希表
PriorityQueue:優(yōu)先級隊列,能有效的刪除最小元素.
5.沒有重復(fù)元素存在的集合HashSet
HashSet通過哈希碼來查找一個元素,這比鏈表的整體查找要迅速得多,但是由此帶來的缺陷時它不能保持插入時的順序.
由于HashSet靠hash碼來識別元素,因此它不能包含重復(fù)元素,當(dāng)集合中已經(jīng)存在值相等的元素時,再次添加同樣的元素HashSet不會發(fā)生變化.
Set<String> hashSet=new HashSet<String>();
hashSet.add("Andy");
hashSet.add("Andy");
hashSet.add("Bill");
hashSet.add("Cindy");
hashSet.add("Bill");
hashSet.add("Cindy");
Iterator<String> iter=hashSet.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
輸出:
Cindy
Andy
Bill
6.自動排序的集合 TreeSet
TreeSet和HashSet類似但它值得一提的一點(diǎn)是無論你以何順序插入元素,元素都能保持正確的排序位置,這時因?yàn)門reeSet是以紅黑樹的形式存儲數(shù)據(jù),在插入時就能找到正確的位置.注意插入TreeSet的元素都要實(shí)現(xiàn)Comparable接口.
例:
SortedSet<String> treeSet=new TreeSet<String>();
treeSet.add("Andy");
treeSet.add("Andy");
treeSet.add("Bill");
treeSet.add("Cindy");
treeSet.add("Bill");
treeSet.add("Cindy");
Iterator<String> iter=treeSet.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
輸出:
Andy
Bill
Cindy
7.優(yōu)先級隊列PriorityQueue
PriorityQueue使用一種名為堆的二叉樹高效的存儲數(shù)據(jù),它檢索數(shù)據(jù)是按排序的順序而插入則隨意,當(dāng)你調(diào)用remove方法時,你總能得到最小的元素.但PriorityQueue并不按序排列它的元素,當(dāng)你遍歷時它是不會排序的,這也沒有必要. 在制作一個按優(yōu)先級執(zhí)行日程安排或事務(wù)安排時你應(yīng)該首先想到PriorityQueue。
例:
PriorityQueue<String> pq=new PriorityQueue<String>();
pq.add("Cindy");
pq.add("Felix");
pq.add("Andy");
pq.add("Bill");
Iterator<String> iter=pq.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
while(!pq.isEmpty()){
System.out.println(pq.remove());
}
8.保持插入順序的哈希表LinkedHashMap
從1.4起Java集合類中多了一個LinkedHashMap,它在HashMap的基礎(chǔ)上增加了一個雙向鏈表,由此LinkedHashMap既能以哈希表的形式存儲數(shù)據(jù),又能保持查詢時的順序.
Map<String,String> members=new LinkedHashMap<String,String>();
members.put("001", "Andy");
members.put("002", "Bill");
members.put("003", "Cindy");
members.put("004", "Dell");
Iterator<String> iter=members.keySet().iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
Iterator<String> iter2=members.values().iterator();
while(iter2.hasNext()){
System.out.println(iter2.next());
}
9.自動按鍵進(jìn)行排序的哈希表TreeMap
Map<String,String> members=new TreeMap<String,String>();
members.put("001", "Andy");
members.put("006", "Bill");
members.put("003", "Cindy");
members.put("002", "Dell");
members.put("004", "Felex");
for(Map.Entry<String,String> entry:members.entrySet()){
System.out.println("Key="+entry.getKey()+" Value="+entry.getValue());
}
10.1 集合工具類Collections的方法unmodifiable
此方法構(gòu)建一個不可更改的集合視圖,當(dāng)使用其修改方法時會拋出一個UnsupportedOperationException
static Collection<E> unmodifiableCollection(Collection<E> c)
static List<E> unmodifiableList (List<E> c)
static Set<E> unmodifiableSet (Set<E> c)
static SortedSet<E> unmodifiableSortedSet (SortedSet<E> c)
static Map<K,V> unmodifiableMap (Map<K,V> c)
static SortedMap<K,V> unmodifiableSortedMap (SortedMap<K,V> c)
10.2 集合工具類Collections的方法synchronized
此方法會返回一個線程安全的集合視圖
static Collection<E> synchronizedCollection(Collection<E> c)
static List<E> synchronizedList (List<E> c)
static Set<E> synchronizedSet (Set<E> c)
static SortedSet<E> synchronizedSortedSet (SortedSet<E> c)
static Map<K,V> synchronizedMap (Map<K,V> c)
static SortedMap<K,V> synchronizedSortedMap (SortedMap<K,V> c)
集合類靜態(tài)類圖

posted on 2008-03-04 08:33 和風(fēng)細(xì)雨 閱讀(457) 評論(0) 編輯 收藏 所屬分類: J2SE