?
1.繼承關系圖
2. 概覽
此類提供? Collection ?接口的骨干實現,以最大限度地減少了實現此接口所需的工作。
構造方法摘要
protected
|
AbstractCollection
()
?
??????????唯一的構造方法。 |
?boolean
|
add
(E?e)
?
??????????確保此 collection 包含指定的元素(可選操作)。 |
|
?boolean
|
addAll
(Collection<? extends?E>?c)
?
??????????將指定 collection 中的所有元素都添加到此 collection 中(可選操作)。 |
|
?void
|
clear
()
?
??????????移除此 collection 中的所有元素(可選操作)。 |
|
?boolean
|
contains
(Object?o)
?
??????????如果此 collection 包含指定的元素,則返回? true 。 |
|
?boolean
|
containsAll
(Collection<?>?c)
?
??????????如果此 collection 包含指定 collection 中的所有元素,則返回? true 。 |
|
?boolean
|
isEmpty
()
?
??????????如果此 collection 不包含元素,則返回? true 。 |
|
abstract ?Iterator<E>
|
iterator
()
?
??????????返回在此 collection 中的元素上進行迭代的迭代器。 |
|
?boolean
|
remove
(Object?o)
?
??????????從此 collection 中移除指定元素的單個實例,如果存在的話(可選操作)。 |
|
?boolean
|
removeAll
(Collection<?>?c)
?
??????????移除此 collection 中那些也包含在指定 collection 中的所有元素(可選操作)。 |
|
?boolean
|
retainAll
(Collection<?>?c)
?
??????????僅保留此 collection 中那些也包含在指定 collection 的元素(可選操作)。 |
|
abstract ?int
|
size
()
?
??????????返回此 collection 中的元素數。 |
|
?Object[]
|
toArray
()
?
??????????返回包含此 collection 中所有元素的數組。 |
|
|
toArray
(T[]?a)
?
??????????返回包含此 collection 中所有元素的數組;返回數組的運行時類型與指定數組的運行時類型相同。 |
|
?String
|
toString
()
?
??????????返回此 collection 的字符串表示形式。 |
?
4.添加元素相關方法源碼
?
// 確保此 collection 包含指定的元素 public boolean add(E e) { throw new UnsupportedOperationException(); } // 將指定 collection 中的所有元素都添加到此 collection 中 public boolean addAll(Collection<? extends E> c) { boolean modified = false; Iterator<? extends E> e = c.iterator(); while (e.hasNext()) { if (add(e.next())) modified = true; } return modified; }
?
?
5.移除元素相關方法源碼
?
// 從此 collection 中移除指定元素的單個實例,如果存在的話 public boolean remove(Object o) { Iterator<E> e = iterator(); if (o == null) { while (e.hasNext()) { if (e.next() == null) { e.remove(); return true; } } } else { while (e.hasNext()) { if (o.equals(e.next())) { e.remove(); return true; } } } return false; } // 移除此 collection 中那些也包含在指定 collection 中的所有元素 public boolean removeAll(Collection<?> c) { boolean modified = false; Iterator<?> e = iterator(); while (e.hasNext()) { if (c.contains(e.next())) { e.remove(); modified = true; } } return modified; } // 僅保留此 collection 中那些也包含在指定 collection 的元素 public boolean retainAll(Collection<?> c) { boolean modified = false; Iterator<E> e = iterator(); while (e.hasNext()) { if (!c.contains(e.next())) { e.remove(); modified = true; } } return modified; } // 移除此 collection 中的所有元素 public void clear() { Iterator<E> e = iterator(); while (e.hasNext()) { e.next(); e.remove(); } }
?
?
6.查找相關方法源代碼
?
// 如果此 collection 包含指定的元素,則返回 true public boolean contains(Object o) { Iterator<E> e = iterator(); if (o == null) { while (e.hasNext()) if (e.next() == null) return true; } else { while (e.hasNext()) if (o.equals(e.next())) return true; } return false; } // 如果此 collection 包含指定 collection 中的所有元素,則返回 true public boolean containsAll(Collection<?> c) { Iterator<?> e = c.iterator(); while (e.hasNext()) if (!contains(e.next())) return false; return true; } // 返回此 collection 中的元素數 public abstract int size(); // 如果此 collection 不包含元素,則返回 true public boolean isEmpty() { return size() == 0; }
?
?
7.轉換相關方法源代碼
?
// 返回在此 collection 中的元素上進行迭代的迭代器 public abstract Iterator<E> iterator(); // 返回包含此 collection 中所有元素的數組 public Object[] toArray() { // Estimate size of array; be prepared to see more or fewer elements Object[] r = new Object[size()]; Iterator<E> it = iterator(); for (int i = 0; i < r.length; i++) { if (!it.hasNext()) // fewer elements than expected return Arrays.copyOf(r, i); r[i] = it.next(); } return it.hasNext() ? finishToArray(r, it) : r; } // 返回包含此 collection 中所有元素的數組;返回數組的運行時類型與指定數組的運行時類型相同 public <T> T[] toArray(T[] a) { // Estimate size of array; be prepared to see more or fewer elements int size = size(); T[] r = a.length >= size ? a : (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size); Iterator<E> it = iterator(); for (int i = 0; i < r.length; i++) { if (!it.hasNext()) { // fewer elements than expected if (a != r) return Arrays.copyOf(r, i); r[i] = null; // null-terminate return r; } r[i] = (T) it.next(); } return it.hasNext() ? finishToArray(r, it) : r; } // private static <T> T[] finishToArray(T[] r, Iterator<?> it) { int i = r.length; while (it.hasNext()) { int cap = r.length; if (i == cap) { int newCap = ((cap / 2) + 1) * 3; if (newCap <= cap) { // integer overflow if (cap == Integer.MAX_VALUE) throw new OutOfMemoryError("Required array size too large"); newCap = Integer.MAX_VALUE; } r = Arrays.copyOf(r, newCap); } r[i++] = (T) it.next(); } // trim if overallocated return (i == r.length) ? r : Arrays.copyOf(r, i); } // 返回此 collection 的字符串表示形式 public String toString() { Iterator<E> i = iterator(); if (!i.hasNext()) return "[]"; StringBuilder sb = new StringBuilder(); sb.append('['); for (;;) { E e = i.next(); sb.append(e == this ? "(this Collection)" : e); if (!i.hasNext()) return sb.append(']').toString(); sb.append(", "); } }
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
