c)將指定collection中的所有元素都添加到此collection中(可選操作)。voidcl" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Java集合2:類 AbstractCollection及源碼

系統(tǒng) 2067 0

?

1.繼承關(guān)系圖


Java集合2:類 AbstractCollection及源碼
?
?

2. 概覽

此類提供? Collection ?接口的骨干實現(xiàn),以最大限度地減少了實現(xiàn)此接口所需的工作。


Java集合2:類 AbstractCollection及源碼
3.方法

構(gòu)造方法摘要
protected AbstractCollection () ?
??????????唯一的構(gòu)造方法。
? 方法摘要
?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 中的元素上進(jìn)行迭代的迭代器。
?boolean remove (Object?o) ?
??????????從此 collection 中移除指定元素的單個實例,如果存在的話(可選操作)。
?boolean removeAll (Collection<?>?c) ?
??????????移除此 collection 中那些也包含在指定 collection 中的所有元素(可選操作)。
?boolean retainAll (Collection<?>?c) ?
??????????僅保留此 collection 中那些也包含在指定 collection 的元素(可選操作)。
abstract ?int size () ?
??????????返回此 collection 中的元素數(shù)。
?Object[] toArray () ?
??????????返回包含此 collection 中所有元素的數(shù)組。
<T> T[]
toArray (T[]?a) ?
??????????返回包含此 collection 中所有元素的數(shù)組;返回數(shù)組的運行時類型與指定數(shù)組的運行時類型相同。
?String toString () ?
??????????返回此 collection 的字符串表示形式。

?

4.添加元素相關(guān)方法源碼

?

      	// 確保此 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.移除元素相關(guān)方法源碼

?

      	// 從此 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.查找相關(guān)方法源代碼

?

      	// 如果此 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 中的元素數(shù)
	public abstract int size();

	// 如果此 collection 不包含元素,則返回 true
	public boolean isEmpty() {
		return size() == 0;
	}
    

?

?

7.轉(zhuǎn)換相關(guān)方法源代碼

?

      	// 返回在此 collection 中的元素上進(jìn)行迭代的迭代器
	public abstract Iterator<E> iterator();

	// 返回包含此 collection 中所有元素的數(shù)組
	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 中所有元素的數(shù)組;返回數(shù)組的運行時類型與指定數(shù)組的運行時類型相同
	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(", ");
		}
	}
    

?

?

Java集合2:類 AbstractCollection及源碼


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 深夜a| 毛片观看网址 | 综合久久综合久久 | 99热精品国产三级在线观看 | 99国产欧美久久精品 | 日一级片| 成人在线视频免费 | 99国产精品久久久久久久日本 | 久久精品国产6699国产精 | 99精品网 | 久久伊人久久亚洲综合 | 91亚洲精品一区二区福利 | 国产高清福利91成人 | 老子影院伦不卡欧美 | 中文字幕日韩精品亚洲七区 | 国内精品久久久久影院一蜜桃 | 日日操狠狠干 | 第一福利在线视频 | 日日私人影院 | 色综合天天综合网国产成人 | 夜夜操夜夜 | 一本大道香蕉中文在线高清 | 偷偷操99 | 久久精品国产99久久无毒不卡 | 99网站 | 国产精品青草久久久久婷婷 | 精品久久久久久中文字幕2017 | 天天干在线影院 | 男人爱看的网站 | 精品一区二区三区视频在线观看 | 亚洲黄色大片 | 97色伦图片97色伦图影院久久 | 99热精品久久只有精品黑人 | 久久精品国产主播一区二区 | 亚洲sss视频| 国产精品a人片在线观看 | 国产成人综合亚洲动漫在线 | 在线视频一区二区日韩国产 | 国产一久久香蕉国产线看观看 | 亚洲一区二区三区视频 | julia在线播放|