![Java教程CommonsCollections學(xué)習(xí)筆記(一)_第1頁](http://file4.renrendoc.com/view/56cabd6544c7b7c02fa23fc66b722be8/56cabd6544c7b7c02fa23fc66b722be81.gif)
![Java教程CommonsCollections學(xué)習(xí)筆記(一)_第2頁](http://file4.renrendoc.com/view/56cabd6544c7b7c02fa23fc66b722be8/56cabd6544c7b7c02fa23fc66b722be82.gif)
![Java教程CommonsCollections學(xué)習(xí)筆記(一)_第3頁](http://file4.renrendoc.com/view/56cabd6544c7b7c02fa23fc66b722be8/56cabd6544c7b7c02fa23fc66b722be83.gif)
![Java教程CommonsCollections學(xué)習(xí)筆記(一)_第4頁](http://file4.renrendoc.com/view/56cabd6544c7b7c02fa23fc66b722be8/56cabd6544c7b7c02fa23fc66b722be84.gif)
![Java教程CommonsCollections學(xué)習(xí)筆記(一)_第5頁](http://file4.renrendoc.com/view/56cabd6544c7b7c02fa23fc66b722be8/56cabd6544c7b7c02fa23fc66b722be85.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quá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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度講師培訓(xùn)品牌合作推廣合同
- 電商平臺用戶體驗的持續(xù)優(yōu)化策略
- 生產(chǎn)線效率提升與資源調(diào)度分析
- 生產(chǎn)現(xiàn)場5S管理提升員工素質(zhì)的途徑
- 現(xiàn)代城市雕塑與夜間光影藝術(shù)的結(jié)合
- 現(xiàn)代商業(yè)環(huán)境下的心理調(diào)適與中醫(yī)養(yǎng)生法
- 現(xiàn)代農(nóng)業(yè)裝備技術(shù)教育與創(chuàng)新培訓(xùn)案例
- 2025年度建筑泥工工程勞務(wù)及綠色建筑認(rèn)證服務(wù)合同
- 生日慶典致辭15篇
- 生物科技在醫(yī)療旅游中的應(yīng)用
- 2023年上海青浦區(qū)區(qū)管企業(yè)統(tǒng)一招考聘用筆試題庫含答案解析
- 2023年高一物理期末考試卷(人教版)
- 2023版押品考試題庫必考點含答案
- 植物之歌觀后感
- 空氣能熱泵安裝示意圖
- 建筑工程施工質(zhì)量驗收規(guī)范檢驗批填寫全套表格示范填寫與說明
- 2020年中秋國慶假日文化旅游市場安全生產(chǎn)檢查表
- 辦公家具項目實施方案、供貨方案
- 七年級英語下冊閱讀理解10篇
- 節(jié)后開工收心會
- 設(shè)計質(zhì)量、進度保證措施
評論
0/150
提交評論