java學習之集合類 Collection接口

Collection 接口是 List 接口和 Set 接口的父接口,一般狀況下不被直接使用。Collection 接口定義了一些通用的方法,經過這些方法能夠實現對集合的基本操做。由於 List 接口和 Set 接口繼承自 Collection 接口,因此也能夠調用這些方法。

本節將介紹 Collection 接口中經常使用的方法,如表 1 所示。

java

表1 Collection接口的經常使用方法
方法名稱 說明
boolean add(E e) 向集合中添加一個元素,E 是元素的數據類型
boolean addAll(Collection c) 向集合中添加集合 c 中的全部元素
void clear() 刪除集合中的全部元素
boolean contains(Object o) 判斷集合中是否存在指定元素
boolean containsAll(Collection c) 判斷集合中是否包含集合 c 中的全部元素
boolean isEmpty() 判斷集合是否爲空
Iterator<E>iterator() 返回一個 Iterator 對象,用於遍歷集合中的元素
boolean remove(Object o) 從集合中刪除一個指定元素
boolean removeAll(Collection c) 從集合中刪除全部在集合 c 中出現的元素
boolean retainAll(Collection c) 僅僅保留集合中全部在集合 c 中出現的元素
int size() 返回集合中元素的個數
Object[] toArray() 返回包含此集合中全部元素的數組

例 1

通過前面的介紹,咱們知道 Collection 是很是重要的一個接口,在表 1 中列出了其經常使用方法。本案例將編寫一個簡單的程序,演示如何使用 Collection 接口向集合中添加方法。具體實現代碼以下:數組

public static void main(Strmg[] args)
{
    ArrayList list1=new ArrayList();    //建立集合 iist1
    ArrayList list2=new ArrayList();    //建立集合 Iist2
    list1.add("one");    //向 list1 添加一個元素
    list1.add("two");    //向 list1 添加一個元素
    list2.addAll(list1);    //將 list1 的全部元素添加到 list2
    list2.add("three");    //向 list2 添加一個元素
    System.out.println("list2 集合中的元素以下:");
    Iterator it1=list2.iterator();
    while(it1.hasNext())
    {
        System.out.print(it1.next()+"、");
    }
}

因爲 Collection 是接口,不能對其實例化,因此上述代碼中使用了 Collection 接口的 ArrayList 實現類來調用 Collection 的方法。add() 方法能夠向 Collection 中添加一個元素,而調用 addAll() 方法能夠將指定 Collection 中的全部元素添加到另外一個 Collection 中。

代碼建立了兩個集合 list1 和 list2,而後調用 add() 方法向 list1 中添加了兩個元素,再調用 addAll() 方法將這兩個元素添加到 list2 中。接下來又向 list2 中添加了一個元素,最後輸出 list2 集合中的全部元素,結果以下:對象

list2 集合中的元素以下:
one、two、three、

例 2

建立一個案例,演示 Collection 集合中 size()、remove() 和 removeAll() 方法的應用。具體代碼以下:blog

public static void main(String[] args)
{
    ArrayList list1=new ArrayList();    //建立集合 list1
    ArrayList list2=new ArrayList();    //建立集合 list2
    list1.add("one");
    list1.add("two");
    list1.add("three");
    System.out.println("list1 集合中的元素數量:"+list1.size());    //輸出list1中的元素數量
    list2.add("two");
    list2.add("four");
    list2.add("six");
    System.out.println("list2 集合中的元素數量:"+list2.size());     //輸出list2中的元素數量
    list2.remove(2);    //刪除第 3 個元素
    System.out.println("\nremoveAll() 方法以後 list2 集合中的元素數量:"+list2.size());
    System.out.println("list2 集合中的元素以下:");
    Iterator it1=list2.iterator();
    while(it1.hasNext())
    {
        System.out.print(it1.next()+"、");
    }
    list1.removeAll(list2);
    System.out.println("\nremoveAll() 方法以後 list1 集合中的元素數量:"+list1.size());
    System.out.println("list1 集合中的元素以下:");
    Iterator it2=list1.iterator();
    while(it2.hasNext())
    {
        System.out.print(it2.next()+"、");
    }
}

list2 集合在調用 remove(2) 方法刪除第 3 個元素以後剩下了 two 和 four。list1.removeAll(list2) 語句會從 list1 中將 list1 和 list2 中相同的元素刪除,即刪除 two 元素。最後輸出結果以下:繼承

list1 集合中的元素數量:3
Iist2 集合中的元素數量:3
remove() 方法以後 list2 集合中的元素數量:2
list2 集合中的元素以下:
two、four、
removeAH() 方法以後 list1 集合中的元素數量:2
list1 集合中的元素以下:
one、 three、
相關文章
相關標籤/搜索