我的原則:先會用再說,內部慢慢來。
學以致用,根據場景學源碼
一、架構
1.1 UML 圖
=== 點擊查看top目錄 ===
1.2 子類UML圖
1.3 常用子類UML圖
- 這裏要注意,Map跟Collection沒有直接關係,更不是 Collection 的子類。
=== 點擊查看top目錄 ===
二、Iterable 接口
2.1 源碼
public interface Iterable<T> {
Iterator<T> iterator();
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
default Spliterator<T> spliterator() {
return Spliterators.spliteratorUnknownSize(iterator(), 0);
}
}
=== 點擊查看top目錄 ===
2.2 方法解析
方法 |
描述 |
iterator |
返回迭代類 Iterator,可以對Collection 內的元素進行迭代處理 |
forEach |
default 方法,傳入消費類Consumer,對全體元素進行一波消費。jdk1.8之後,允許interface 定義default 方法與 static 方法 |
spliterator |
返回 IteratorSpliterator 類 |
=== 點擊查看top目錄 ===
三、 Iterator 接口
3.1 源碼
public interface Iterator<E> {
boolean hasNext();
E next();
default void remove() {
throw new UnsupportedOperationException("remove");
}
default void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (hasNext())
action.accept(next());
}
}
=== 點擊查看top目錄 ===
3.2 方法解析
方法 |
描述 |
hasNext |
後面是否還有元素 |
remove |
移除元素 ,default 方法,需要被子類重寫 |
forEachRemaining |
傳入 Consumer,處理集合中剩下的所有元素,default 方法 |
=== 點擊查看top目錄 ===
四、Collection 接口
- Collection 接口,底下有一堆實現類,比如:List,Set ,Queue等等
4.1 源碼剖析
public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
<T> T[] toArray(T[] a);
boolean add(E e);
boolean remove(Object o);
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean removeAll(Collection<?> c);
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
final Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
}
boolean retainAll(Collection<?> c);
void clear();
boolean equals(Object o);
int hashCode();
@Override
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, 0);
}
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}
default Stream<E> parallelStream() {
return StreamSupport.stream(spliterator(), true);
}
}
=== 點擊查看top目錄 ===
4.2 方法列表
方法 |
描述 |
int size |
查詢 Collection 內 element 數量 |
boolean isEmpty |
檢查 Collection 內是否是空的 |
boolean contains(Object o); |
檢查 Collection 內是否包含某個元素 |
Iterator iterator(); |
返回Iterator 接口 |
Object[] toArray(); |
Collection 轉 數組Array |
T[] toArray(T[] a); |
Collection 轉 數組Array |
boolean add(E e); |
Collection 插入元素 |
boolean remove(Object o); |
Collection 移除元素 |
boolean containsAll(Collection<?> c); |
Collection 是否包含 Collection c內的全部元素在 |
boolean addAll(Collection<? extends E> c); |
一個Collection 塞入另一個 Collection |
boolean removeAll(Collection<?> c); |
Collection 把 Collection c內的全部元素移除 |
default boolean removeIf(Predicate<? super E> filter) |
傳入判斷條件Predicate,按條件刪除 |
boolean retainAll(Collection<?> c); |
取兩個Collection 的交集,原Collection - Collection c |
void clear(); |
清掉 Collection ,往往是爲了加快 GC |
boolean equals(Object o); |
判斷兩個Collection是否equals,需要重寫該方法 |
int hashCode(); |
hashCode |
default Spliterator spliterator() |
default 方法,返回 Spliterator |
default Stream stream() |
default 方法,拿到Stream 流 |
default Stream parallelStream() |
default 方法,拿到 parallelStream 流 |
=== 點擊查看top目錄 ===
4.3 常用 Collection 子類
接口 |
子類 |
Set |
HashSet 、TreeSet |
List |
Vector 、ArrayList 、LinkedList |
Queue |
SynchronousQueue 、PriorityBlockingQueue 、 DelayQueue 、ArrayBlockingQueue、LinkedBlockingQueue |
=== 點擊查看top目錄 ===
五、AbstractCollection 抽象類
- 相比 Collection 接口,實現了 N 若干個方法
相比 Collection 接口實現的方法 |
isEmpty() |
contains(Object o) |
toArray() |
toArray(T[] a) |
remove(Object o) |
containsAll(Collection<?> c) |
addAll(Collection<? extends E> c) |
removeAll(Collection<?> c) |
retainAll(Collection<?> c) |
clear() |
toString() |
六、List 接口
public interface List<E> extends Collection<E> {}
6.1 特點
- 集合內元素有序(排列順序自己定)
- 元素可以重複
- 可以有N個null 元素
6.2 List 方法列表
6.2.1 重寫 Collection 方法
方法 |
描述 |
int size(); |
同 Collection |
boolean isEmpty(); |
同 Collection |
boolean contains(Object o); |
同 Collection |
Iterator iterator(); |
同 Collection |
Object[] toArray(); |
同 Collection |
T[] toArray(T[] a); |
同 Collection |
boolean add(E e); |
同 Collection |
boolean remove(Object o); |
同 Collection |
boolean containsAll(Collection<?> c); |
同 Collection |
boolean addAll(Collection<? extends E> c); |
同 Collection |
boolean removeAll(Collection<?> c); |
同 Collection |
boolean retainAll(Collection<?> c); |
同 Collection |
void clear(); |
同 Collection |
boolean equals(Object o); |
同 Collection |
int hashCode(); |
同 Collection |
default Spliterator spliterator() |
同 Collection // 重寫,Spliterator.ORDERED |
6.2.2 相比 Collection 增加的方法 (12個)
方法 |
描述 |
boolean addAll(int index, Collection<? extends E> c); |
比老的allAll多了一個下標index |
E get(int index); |
獲取某個 index 下 Element |
E set(int index, E element); |
設置某個 index 下的 Element |
void add(int index, E element); |
在某個 index 後插入Element |
E remove(int index); |
移除某個 index 的Element |
int indexOf(Object o); |
從0 到 N,查看某個Element的下標 |
int lastIndexOf(Object o); |
從 N 到0 ,查看某個Element的下標 |
ListIterator listIterator(); |
返回一個雙向 ListIterator |
ListIterator listIterator(int index); |
從某個 index 往後截取全部元素,組成雙向 ListIterator |
List subList(int fromIndex, int toIndex); |
切割List,組成雙向 ListIterator |
default void replaceAll(UnaryOperator operator) |
輸出操作函數,對list內全部元素進行處理 |
default void sort(Comparator<? super E> c) |
輸入 Comparator 排列器,對list內全部元素進行排隊 |
6.2.3 相比 Collection 少了的方法 (3個)
方法 |
描述 |
default boolean removeIf(Predicate<? super E> filter) |
傳入判斷條件Predicate,按條件刪除 |
default Stream stream() |
default 方法,拿到Stream 流 |
default Stream parallelStream() |
default 方法,拿到 parallelStream 流 |
=== 點擊查看top目錄 ===
七、Queue 接口
public interface Queue<E> extends Collection<E> {}
7.1 特點
- 先進先出
- 元素可以重複
-
7.2 Queue 方法列表
7.2.1 重寫 Collection 方法(無)
無
7.2.2 相比 Collection 增加的方法 (6個)
方法 |
描述 |
boolean add(E e); |
添加元素,失敗拋 IllegalArgumentException |
boolean offer(E e); |
添加元素,失敗返回 false |
E remove(); |
移除元素,隊列內沒元素拋 NoSuchElementException |
E poll(); |
移除元素,隊列內沒元素返回 null |
E element(); |
取出隊頭元素,隊列內沒元素拋 NoSuchElementException |
E peek(); |
取出隊頭元素,隊列內沒元素返回 false |
7.2.3 相比 Collection 少了全部方法
=== 點擊查看top目錄 ===
八、Set 接口
public interface Set<E> extends Collection<E> {}
8.1 特點
- 集合內元素無序
- 元素不可以重複
- 可以有一個null 元素
8.2 Set 方法列表
8.2.1 重寫 Collection 方法
方法 |
描述 |
int size(); |
同 Collection |
boolean isEmpty(); |
同 Collection |
boolean contains(Object o); |
同 Collection |
Iterator iterator(); |
同 Collection |
Object[] toArray(); |
同 Collection |
T[] toArray(T[] a); |
同 Collection |
boolean add(E e); |
同 Collection |
boolean remove(Object o); |
同 Collection |
boolean containsAll(Collection<?> c); |
同 Collection |
boolean addAll(Collection<? extends E> c); |
同 Collection |
boolean retainAll(Collection<?> c); |
同 Collection |
boolean removeAll(Collection<?> c); |
同 Collection |
void clear(); |
同 Collection |
boolean equals(Object o); |
同 Collection |
int hashCode(); |
同 Collection |
8.2.2 新增 Collection 方法 (無)
無
8.2.3 相比 Collection 少了的方法(4個)
方法 |
描述 |
default boolean removeIf(Predicate<? super E> filter) |
傳入判斷條件Predicate,按條件刪除 |
default Spliterator spliterator() |
default 方法,返回 Spliterator |
default Stream stream() |
default 方法,拿到Stream 流 |
default Stream parallelStream() |
default 方法,拿到 parallelStream 流 |
=== 點擊查看top目錄 ===
九、番外篇
下一章節:【Java Collection】List 剖析(二)