版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、Java 教程 :CommonsCollections 學(xué)習(xí)筆記(一)public interface Bag extends Collectionint getCount(Object object);boolean add(Object object);boolean add(Object object, int nCopies);boolean remove(Object object);boolean remove(Object object, int nCopies);Set uniqueSet();int size();boolean containsAll(Collection
2、coll);boolean removeAll(Collection coll);boolean retainAll(Collection coll);Iterator iterator();public interface SortedBag extends Bagpublic Comparator comparator();public Object first();public Object last();public abstract class DefaultMapBag implements Bagprivate Map _map = null;/ 底層數(shù)據(jù)存儲區(qū)private i
3、nt _total = 0; / 元素總個數(shù)private int _mods = 0;/ 修改次數(shù)public DefaultMapBag() protected DefaultMapBag(Map map) setMap(map);public boolean add(Object object) return add(object, 1);public boolean add(Object object, int nCopies) _mods+;if (nCopies 0) int count = (nCopies + getCount(object);_map.put(object,
4、new Integer(count);_total += nCopies;return (count = nCopies); else return false;public boolean addAll(Collection coll) boolean changed = false;Iterator i = coll.iterator();while (i.hasNext() boolean added = add(i.next();changed = changed | added;return changed;public void clear() _mods+;_map.clear(
5、);_total = 0;public boolean contains(Object object) return _map.containsKey(object);public boolean containsAll(Collection coll) return containsAll(new HashBag(coll);public boolean containsAll(Bag other) boolean result = true;Iterator i = other.uniqueSet().iterator();while (i.hasNext() Object current
6、 = i.next();boolean contains = getCount(current) = other.getCount(current);result = result & contains;return result;public boolean equals(Object object) if (object = this) return true;if (object instanceof Bag = false) return false;Bag other = (Bag) object;if (other.size() != size() return false;for
7、 (Iterator it = _map.keySet().iterator(); it.hasNext();) Object element = it.next();if (other.getCount(element) != getCount(element) return false;return true;public int hashCode() return _map.hashCode();public boolean isEmpty() return _map.isEmpty(); public Iterator iterator() return new BagIterator
8、(this, extractList().iterator();原始迭代器static class BagIterator implements Iterator private DefaultMapBag _parent = null;private Iterator _support = null;/private Object _current = null;/ 當(dāng)前元素private int _mods = 0;public BagIterator(DefaultMapBag parent, Iteratorsupport) _parent = parent;_support = su
9、pport;_current = null;_mods = parent.modCount();public boolean hasNext() return _support.hasNext();public Object next() if (_parent.modCount() != _mods) throw new ConcurrentModificationException();_current = _support.next();return _current;public void remove() if (_parent.modCount() != _mods) throw
10、new ConcurrentModificationException();_support.remove();_parent.remove(_current, 1);_mods+;public boolean remove(Object object) return remove(object, getCount(object);public boolean remove(Object object, int nCopies) _mods+;boolean result = false;int count = getCount(object);if (nCopies nCopies) _ma
11、p.put(object, new Integer(count - nCopies);result = true;_total -= nCopies; else / count 0 & count = i/ need toremove allresult = (_map.remove(object) != null);_total -= count;return result;public boolean removeAll(Collection coll) boolean result = false;if (coll != null) Iterator i = coll.iterator(
12、);while (i.hasNext() boolean changed = remove(i.next(), 1);result = result | changed;return result;public boolean retainAll(Collection coll) return retainAll(new HashBag(coll);public boolean retainAll(Bag other) boolean result = false;Bag excess = new HashBag();Iterator i = uniqueSet().iterator();wh
13、ile (i.hasNext() Object current = i.next();int myCount = getCount(current);int otherCount = other.getCount(current);if (1 = otherCount & otherCount 0; index-) result.add(current);return result;private int modCount() return _mods;public String toString() StringBuffer buf = new StringBuffer();buf.appe
14、nd();Iterator i = uniqueSet().iterator();while (i.hasNext() Object current = i.next();int count = getCount(current);buf.append(count);buf.append(:);buf.append(current);if (i.hasNext() buf.append(,);buf.append();return buf.toString();public class HashBag extends DefaultMapBag implements Bagpublic Has
15、hBag() super(new HashMap();public HashBag(Collection coll) this();addAll(coll);public class TreeBag extends DefaultMapBag implements SortedBagpublic TreeBag() super(new TreeMap();public TreeBag(Comparator comparator) super(new TreeMap(comparator);public TreeBag(Collection coll) this();addAll(coll);p
16、ublic Object first() return (SortedMap) getMap().firstKey();public Object last() return (SortedMap) getMap().lastKey();public Comparator comparator() return (SortedMap) getMap().comparator();使用 decorate 模式的 Bag 工具類public class BagUtils/*An empty unmodifiable bag./public static final Bag EMPTY_BAG =
17、UnmodifiableBag.decorate(new HashBag();/*An empty unmodifiable sorted bag./public static final Bag EMPTY_SORTED_BAG = UnmodifiableSortedBag.decorate(new TreeBag();public BagUtils() / 這里按常理不應(yīng)該是public ,應(yīng)該是private 才對,作者應(yīng)該有其他地方要用到吧public static Bag synchronizedBag(Bag bag) return SynchronizedBag.decorat
18、e(bag);public static Bag unmodifiableBag(Bag bag) return UnmodifiableBag.decorate(bag);public static Bag predicatedBag(Bag bag, Predicate predicate) return PredicatedBag.decorate(bag, predicate);public static Bag typedBag(Bag bag, Class type) return TypedBag.decorate(bag, type);public static Bag tra
19、nsformedBag(Bag bag,Transformer transformer) return TransformedBag.decorate(bag, transformer);public static SortedBagsynchronizedSortedBag(SortedBag bag) return SynchronizedSortedBag.decorate(bag);public static SortedBagunmodifiableSortedBag(SortedBag bag) return UnmodifiableSortedBag.decorate(bag);public static SortedBagpredicatedSortedBag(SortedBag bag, Predicate predicate) return PredicatedSortedBag.decorate(bag, predicate);public static SortedBag typedSortedBag(SortedBagbag, Class
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度天然氣儲備庫安全運營管理合同
- 二零二五年度工業(yè)設(shè)備安裝與調(diào)試服務(wù)合同3篇
- 二零二五版快遞企業(yè)快遞物品安全防護(hù)合同大全3篇
- 2025年度城市綜合體門頭廣告品牌形象改造合同3篇
- 2025年度拆遷安置房交易全程跟蹤服務(wù)合同協(xié)議3篇
- 個人消費性借款合同(2024版)9篇
- 二零二五年度可再生能源發(fā)電特許經(jīng)營合作協(xié)議合同范本
- 二零二五年度醫(yī)療健康信息化運維保障合同2篇
- 2025版商業(yè)物業(yè)安全責(zé)任書(含應(yīng)急預(yù)案)3篇
- 2025年度個性化產(chǎn)后恢復(fù)與新生兒護(hù)理個人月嫂服務(wù)協(xié)議4篇
- 《裝配式蒸壓加氣混凝土外墻板保溫系統(tǒng)構(gòu)造》中
- T-CSTM 01124-2024 油氣管道工程用工廠預(yù)制袖管三通
- 2019版新人教版高中英語必修+選擇性必修共7冊詞匯表匯總(帶音標(biāo))
- 新譯林版高中英語必修二全冊短語匯總
- 基于自適應(yīng)神經(jīng)網(wǎng)絡(luò)模糊推理系統(tǒng)的游客規(guī)模預(yù)測研究
- 河道保潔服務(wù)投標(biāo)方案(完整技術(shù)標(biāo))
- 品管圈(QCC)案例-縮短接臺手術(shù)送手術(shù)時間
- 精神科病程記錄
- 閱讀理解特訓(xùn)卷-英語四年級上冊譯林版三起含答案
- 清華大學(xué)考博英語歷年真題詳解
- 人教版三年級上冊口算題(全冊完整20份 )
評論
0/150
提交評論