---
# System prepended metadata

title: Collections
tags: [java, learning]

---

# Java Collections
###### 2024/06/12
---

### Collections Framework 
* 處理資料結構
* 集合的大小可以自動改變
* 皆有`toString()`方法，可以直接輸出
* 下圖為架構(每個區塊皆是**interface**)
```mermaid
graph TD
Collection-->Set
Collection-->List
Collection-->Queue
Map-->SortedMap
Set-->SortedSet

style Collection fill:#bbf,stroke:#259,stroke-width:2px
style Map fill:#bbf,stroke:#259,stroke-width:2px
```

### Collection
* 可搭配**for-each**
* 基本method:star:
    * `boolean add(Object o)`
    * `boolean contains(Object o)`
    * `boolean isEmpty()`
    * `Iterator iterator()`
    * `boolean remove(Object o)`
    * `int size()`
* 多元素method
    * `boolean addAll(Collection c)`
    * `void clear()`
    * `boolean containsAll(Collection c)`
    * `boolean removeAll(Collection c)`
    * `boolean retainAll(Collection c)`
        >將包含於c內的元素留下，其他刪除
* 陣列method
    * `Object[] toArray()`
    * `Object[] toArray(Object[] a)`
        >指定存放的陣列，放不下會重新建立
* Collections

### Set
:::info
* 元素不重複
:::
* HashSet & LinkedHashSet
    * 架構
    ```mermaid
    graph TD
    HashSet-->LinkedHashSet
    ```
    * HashSet\<E>
    * LinkedHashSet\<E>
* TreeSet
    :::info
    * 自動排序
    :::
    * method
        * `Object first()`
        * `Object last()`
        * `Object ceiling(Object o)`
        * `Object floor(Object o)`
        * `SortedSet headSet(Object toElement)`
        * `SortedSet subSet(Object fromElement, Object toElement)`
        * `SortedSet(Object fromElement)`

### List
:::info
* 有次序性
* 可以用索引來儲存:star:
* 元素可重複
:::
* method
    * `void add(int index, Object element)`
        >插入
    * `boolean addAll(int index, Collection c)`
    * `Object get(int index)`
    * `int indexOf(Object o)`
    * `int lastIndexOf(Object o)`
    * `Object remove(int index)`
    * `Object set(int index, Object o)`
        >替換
    * `List subList(int fromIndex, int toIndex)`
* ArrayList\<E>
    * 空間管理method
        * `void ensureCapacity(int minCapacity)`
        * `void trimToSize()`
* LinkedList\<E>
    * method
        * 會拋出例外
            * `void addFirst(Object o)`
            * `void addLast(Object o`
            * `Object getFirst()`
            * `Object getLast()`
            * `Object removeFirst()`
            * `Object removeLast()`
        * 會回傳布林值或null值:star:
            * `boolean offerFirst(Object o)`
            * `boolean offerLast(Object o)`
            * `Object peekFirst()`
            * `Object peekLast()`
            * `Object pollFirst()`
            * `Object pollLast()`
    * 同時實作了Queue和List
        * 方便用來實作Queue和Stack

### Map
:::info
* key-value
* 重複加入鍵會取代先前的鍵
:::
* 基本method
    * `boolean isEmpty()`
    * `int size()`
    * `void clear()`
    * `Object put(Object key, Object value)`:star:
    * `void putAll(Map t)`
    * `Object remove(Object key)`:star:
* 集合操作method
    * `boolean containsKey(Object key)`
    * `boolean containsValue(Object value)`
    * `Set entrySet()`
        >"key=value"格式的Set
    * `Object get(Object key)`:star:
    * `Set keySet()`
        >回傳key Set，可搭配**for-each**
    * `Collection values()`
        >回傳value Collection，可搭配**for-each**
* HashSet\<E, E>

### Iterator
:::info
* 可以直接刪除 & 瀏覽Collection中的元素
* 迭代中不可以直接修改Collection，否則會拋出例外
* 等到iterator走到`hasNext=false`時，便可以再次修改Collection
:::
* method
    * `boolean hasNext()`
    * `Object next()`
    * `void remove()`
* `Iterator i = yourCollection.iterator();`

### Collections
```java=
static boolean
addAll(Collection c, Object... elements);

static int
binarySearch(List list, Comparable key);
static int
binarySearch(List list, Object key, Comparator c);

static void
copy(List dest, List src);
// Copies all of the elements from one list into another.

static boolean
disjoint(Collection c1, Collection c2);
// Returns true if the two specified collections have no elements in common.

static void
fill(List list, Object obj);

static int
frequency(Collection c, Object o);

static Object
max(Collection coll);
static Object
max(Collection coll, Comparator comp);

static Object
min(Collection coll);
static Object
min(Collection coll, Comparator comp);

static List
nCopies(int n, Object o);

static boolean
replaceAll(List list, Object oldVal, Object newVal);

static void
reverse(List list);

static void
sort(List list, Comparator c);

static void
swap(List list, int i, int j);
```