Collection
1.collection概念
一個集合(有時稱爲容器)只是一個將多個元素分組到一個單元中的對象。集合用於存儲,檢索,操作和傳輸聚合數據。典型地,它們表示形成自然組的數據項目,例如撲克牌(卡片集合),郵件文件夾(字母集合)或電話目錄(名稱到電話號碼的映射)。如果您已經使用了Java編程語言 - 或者其他任何編程語言 - 則您已經熟悉了這些集合。
2.Collections Framework概念
集合框架是用於表示和操作集合的統一體系結構。所有的集合框架包含以下內容:
接口:這些是表示集合的抽象數據類型。接口允許集合獨立於其表示的細節被操縱。在面向對象的語言中,接口通常形成一個層次結構。
Note that Iterator.remove isthe only safe way to modify a collection during iteration; thebehavior is unspecified if the underlying collection is modified in any otherway while the iteration is in progress.
Use Iterator instead ofthe for-each construct when you need to:
- Remove the current element. The for-each construct hides the iterator, so you cannot call remove. Therefore, the for-each construct is not usable for filtering.
- Iterate over multiple collections in parallel.
staticvoid filter(Collection<?> c) {
for (Iterator<?> it = c.iterator();it.hasNext(); )
if (!cond(it.next()))
it.remove();
}
importjava.util.*;
publicclass FindDups2 {
public static void main(String[] args) {
Set<String> uniques = newHashSet<String>();
Set<String> dups = new HashSet<String>();
for (String a : args)
if (!uniques.add(a))
dups.add(a);
// Destructive set-difference
uniques.removeAll(dups);
System.out.println("Uniquewords: " + uniques);
System.out.println("Duplicatewords: " + dups);
}
}