程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java的collections集合

java的collections集合

編輯:關於JAVA

下面這張表格(表一)總結了用一個集合能做的所有事情(亦可對Set和List做同樣的事情,盡管List還提供了一些額外的功能)。Map不是從Collection繼承的,所以要單獨對待。

boolean add(Object) *保證集合內包含了自變量。如果它沒有添加自變量,就返回false(假)
boolean addAll(Collection) *添加自變量內的所有元素。如果沒有添加元素,則返回true(真)
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() 返回包含了集合內所有元素的一個數組

Boolean add(Object)
 

*Ensures that the Collection contains the argument. Returns false if it doesn’t add the argument.
 

Boolean addAll(Collection)
 

*Adds all the elements in the argument. Returns true if any elements were added.
 

void clear()
 

*Removes all the elements in the Collection.
 

Boolean contains(Object)
 

True if the Collection contains the argument.
 

Boolean containsAll(Collection)
 

True if the Collection contains all the elements in the argument.
 

Boolean isEmpty()
 

True if the Collection has no elements.
 

Iterator iterator()
 

Returns an Iterator that you can use to move through the elements in the Collection.
 

Boolean remove(Object)
 

*If the argument is in the Collection, one instance of that element is removed. Returns true if a removal occurred.
 

Boolean removeAll(Collection)
 

*Removes all the elements that are contained in the argument. Returns true if any removals occurred.
 

Boolean retainAll(Collection)
 

*Retains only elements that are contained in the argument (an “intersection” from set theory). Returns true if any changes occurred.
 

int size()
 

Returns the number of elements in the Collection.
 

Object[] toArray()
 

Returns an array containing all the elements in the Collection.
 

Object[] toArray(Object[] a)
 

Returns an array containing all the elements in the Collection, whose type is that of the array a rather than plain Object (you must cast the array to the right type).
 


 

*This is an “optional” method, which means it might not be implemented by a particular Collection. If not, that method throws an UnsupportedOperationException. Exceptions will be covered in Chapter 9.
 


表一

*這是一個“可選的”方法,有的集合可能並未實現它。若確實如此,該方法就會遇到一個UnsupportedOperatiionException,即一個“操作不支持”違例,詳見第9章。

下面這個例子向大家演示了所有方法。同樣地,它們只對從集合繼承的東西有效,一個ArrayList作為一種“不常用的分母”使用:
 

//: Collection1.java
// Things you can do with all Collections
package c08.newcollections;
import java.util.*;

public class Collection1 {
  // Fill with 'size' elements, start
  // counting at 'start':
  public static Collection 
  fill(Collection c, int start, int size) {
    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 = c.iterator(); 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(
      "c.contains(\"4\") = " + c.contains("4"));
    // Is a Collection in this Collection
    System.out.println(
      "c.containsAll(newCollection()) = " + 
      c.containsAll(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);
  }
} ///:~

通過第一個方法,我們可用測試數據填充任何集合。在當前這種情況下,只是將int轉換成String。第二個方法將在本章其余的部分經常采用。
newCollection()的兩個版本都創建了ArrayList,用於包含不同的數據集,並將它們作為集合對象返回。所以很明顯,除了Collection接口之外,不會再用到其他什麼。
print()方法也會在本節經常用到。由於它用一個反復器(Iterator)在一個集合內遍歷,而任何集合都可以產生這樣的一個反復器,所以它適用於List和Set,也適用於由一個Map生成的Collection。
main()用簡單的手段顯示出了集合內的所有方法。
在後續的小節裡,我們將比較List,Set和Map的不同實現方案,同時指出在各種情況下哪一種方案應成為首選(帶有星號的那個)。大家會發現這裡並未包括一些傳統的類,如Vector,Stack以及Hashtable等。因為不管在什麼情況下,新集合內都有自己首選的類。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved