# Collection 介紹

程式中經常有將物件收集在一起以便管理使用的需求,
一個實作 Collection 介面的物件可以提供這種服務。

使用 Collection 的優點:

  1. 一致的 API
  2. 減少工作量
  3. 增進程式碼的速度跟質量

# Collection 架構圖

# List 介面

# ArrayList

  • ArrayList 是基於動態陣列的資料結構
  • ArrayList 有 get()set() 方法,隨機訪問比較快 O(1)
  • ArrayList 對新增和刪除操作 add()remove() 操作需要移動資料
  • ArrayList 浪費空間主要在於在 list 列表的結尾需要預留一定的容量空間
ArrayList演示
public static void main(String[] args)	{
    // 宣告 ArrayList
    ArrayList<Integer> al = new ArrayList<Integer>();
    // 從 list 的最後面新增資料
    for (int i = 1; i <= 5; i++)
        al.add(i);
    // 打印資料
    System.out.println(al);
    // 將索引值為 3 的值 (4) 移除
    al.remove(3);
    System.out.println(al);
    // 歷遍資料 al.size () 可取得資料數
    for (int i = 0; i < al.size(); i++) 
        System.out.print(al.get(i) + " ");
}
輸出
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5

# LinkedList

  • LinkedList 是基於雙向連結串列的資料結構
  • LinkedLsit 的訪問需要從頭開始移動指標 O(n)
  • 對新增和刪除操作 add()remove() 操作,LinkedList 更加快捷
  • LinkedList 的空間花費體現在每一個元素消耗的空間比較大。
LinkedList演示
public static void main(String[] args)	{
    // 宣告 LinkedList
    LinkedList<Integer> ll = new LinkedList<Integer>();
    // 從 list 的最後面新增資料
    for (int i = 1; i <= 5; i++)
        ll.add(i);
    // 打印資料
    System.out.println(ll);
    // 將索引值為 3 的值 (4) 移除
    ll.remove(3);
    System.out.println(ll);
    // 歷遍資料 ll.size () 可取得資料數
    for (int i = 0; i < ll.size(); i++)
        System.out.print(ll.get(i) + " ");
}
輸出
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5

# Vector

public static void main(String[] args){
    // 宣告 Vector
    Vector<Integer> v = new Vector<Integer>();
    // 從 list 的最後面新增資料
    for (int i = 1; i <= 5; i++)
        v.add(i);
    // 打印資料
    System.out.println(v);
    // 將索引值為 3 的值 (4) 移除
    v.remove(3);
    System.out.println(v);
    // 歷遍資料 v.size () 可取得資料數
    for (int i = 0; i < v.size(); i++)
        System.out.print(v.get(i) + " ");
}
輸出
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5

# Queue 介面

# Queue

public static void main(String[] args){
    // 宣告 Queue 
    Queue<Integer> q = new LinkedList<>();
    // 在 Queue 中新增 {0, 1, 2, 3, 4}
    for (int i = 0; i < 5; i++)
        q.add(i);
    // 將資料打印出來
    System.out.println("Elements of queue " + q );
    // 刪除第一筆資料
    int removedele = q.remove();
    System.out.println("removed element: "+ removedele);
    System.out.println(q);
    // 查看第一筆資料
    int head = q.peek();
    System.out.println("head of queue: " + head );
    // 查看 Queue 的資料數
    int size = q.size();
    System.out.println("Size of queue: " + size );
}
Elements of queue [0, 1, 2, 3, 4]
removed element-0
[1, 2, 3, 4]
head of queue-1
Size of queue-4

# PriorityQueue

public static void main(String args[]){
    // 宣告 PriorityQueue
    PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>();
    // 使用.add () 加入元素至 pQueue
    pQueue.add(10); 
    pQueue.add(20); 
    pQueue.add(15); 
    // 查看最上方的元素
    System.out.println(pQueue.peek()); // 10
    // 打印最上方元素,並刪除它
    System.out.println(pQueue.poll()); // 10 X
    // 再次查看最上方元素
    System.out.println(pQueue.peek()); // 15
}
輸出
10
10
15
更新於 閱讀次數

用實際行動犒賞爆肝的我😀

Zrn Ye LinePay

LinePay