下面這張表格總結了用一個集合能做的所有事情(亦可對Set和List做同樣的事情
boolean add(Object) *保證集合內包含了自變量
boolean addAll(Collection) *添加自變量內的所有元素
void clear() *刪除集合內的所有元素
boolean contains(Object) 若集合包含自變量
boolean containsAll(Collection) 若集合包含了自變量內的所有元素
boolean isEmpty() 若集合內沒有元素
Iterator iterator() 返回一個反復器
boolean remove(Object) *如自變量在集合裡
boolean removeAll(Collection) *刪除自變量裡的所有元素
boolean retainAll(Collection) *只保留包含在一個自變量裡的元素(一個理論的
int size() 返回集合內的元素數量
Object[] toArray() 返回包含了集合內所有元素的一個數組
*這是一個
下面這個例子向大家演示了所有方法
//: Collection
// Things you can do with all Collections
package c
import java
public class Collection
// Fill with
// counting at
public static Collection
fill(Collection c
for(int i = start; i < start + size; i++)
c.add(Integer.toString(i));
return c;
}
// Default to a "start" of 0:
public static Collection
fill(Collection c, int size) {
return fill(c, 0, size);
}
// Default to 10 elements:
public static Collection fill(Collection c) {
return fill(c, 0, 10);
}
// Create & upcast to Collection:
public static Collection newCollection() {
return fill(new ArrayList());
// ArrayList is used for simplicity, but it's
// only seen as a generic Collection
// everywhere else in the program.
}
// Fill a Collection with a range of values:
public static Collection
newCollection(int start, int size) {
return fill(new ArrayList(), start, size);
}
// Moving through a List with an iterator:
public static void print(Collection c) {
for(Iterator x = erator(); x.hasNext();)
System.out.print(x.next() + " ");
System.out.println();
}
public static void main(String[] args) {
Collection c = newCollection();
c.add("ten");
c.add("eleven");
print(c);
// Make an array from the List:
Object[] array = c.toArray();
// Make a String array from the List:
String[] str =
(String[])c.toArray(new String[1]);
// Find max and min elements; this means
// different things depending on the way
// the Comparable interface is implemented:
System.out.println("Collections.max(c) = " +
Collections.max(c));
System.out.println("Collections.min(c) = " +
Collections.min(c));
// Add a Collection to another Collection
c.addAll(newCollection());
print(c);
c.remove("3"); // Removes the first one
print(c);
c.remove("3"); // Removes the second one
print(c);
// Remove all components that are in the
// argument collection:
c.removeAll(newCollection());
print(c);
c.addAll(newCollection());
print(c);
// Is an element in this Collection?
System.out.println(
"ntains(\"4\") = " + ntains("4"));
// Is a Collection in this Collection?
System.out.println(
"ntainsAll(newCollection()) = " +
ntainsAll(newCollection()));
Collection c2 = newCollection(5, 3);
// Keep all the elements that are in both
// c and c2 (an intersection of sets):
c.retainAll(c2);
print(c);
// Throw away all the elements in c that
// also appear in c2:
c.removeAll(c2);
System.out.println("c.isEmpty() = " +
c.isEmpty());
c = newCollection();
print(c);
c.clear(); // Remove all elements
System.out.println("after c.clear():");
print(c);
}
} ///:~
通過第一個方法,我們可用測試數據填充任何集合。tW.WIngwIT.cOM在當前這種情況下,只是將int轉換成String。第二個方法將在本章其余的部分經常采用。
newCollection()的兩個版本都創建了ArrayList,用於包含不同的數據集,並將它們作為集合對象返回。所以很明顯,除了Collection接口之外,不會再用到其他什麼。
print()方法也會在本節經常用到。由於它用一個反復器(Iterator)在一個集合內遍歷,而任何集合都可以產生這樣的一個反復器,所以它適用於List和Set,也適用於由一個Map生成的Collection。
main()用簡單的手段顯示出了集合內的所有方法。
在後續的小節裡,我們將比較List,Set和Map的不同實現方案,同時指出在各種情況下哪一種方案應成為首選(帶有星號的那個)。大家會發現這裡並未包括一些傳統的類,如Vector,Stack以及Hashtable等。因為不管在什麼情況下,新集合內都有自己首選的類。
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19591.html