


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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 Bag private Map _map = null;/ 底層數(shù)據(jù)存儲(chǔ)區(qū)private
3、 int _total = 0; / 元素總個(gè)數(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
4、(object, 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
5、+;_map.clear();_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()
6、Object current = 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()
7、 != size() return false;for (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 iterat
8、or() return new BagIterator(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, Iterator support)
9、_parent = parent;_support = support;_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 (_pare
10、nt.modCount() != _mods) throw 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(ob
11、ject);if (nCopies <= 0) result = false; else if (count > nCopies) _map.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 r
12、emoveAll(Collection coll) boolean result = false;if (coll != null) Iterator i = coll.iterator();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
13、 other) boolean result = false;Bag excess = new HashBag();Iterator i = uniqueSet().iterator();while (i.hasNext() Object current = i.next();int myCount = getCount(current);int otherCount = other.getCount(current);if (1 <= otherCount && otherCount <= myCount) excess.add(current, myCount
14、- otherCount); else excess.add(current, myCount);if (!excess.isEmpty() result = removeAll(excess);return result;public Object toArray() return extractList().toArray();public Object toArray(Object array) return extractList().toArray(array);public int getCount(Object object) int result = 0;Integer cou
15、nt = MapUtils.getInteger(_map, object); if (count != null) result = Value();return result;public Set uniqueSet() return UnmodifiableSet.decorate(_map.keySet();public int size() return _total;protected int calcTotalSize() _total = extractList().size();return _total;protected void setMap(Map
16、map) if (map = null | map.isEmpty() = false) throw new IllegalArgumentException("The map must be non-null and empty");_map = map;protected Map getMap() return _map;private List extractList() List result = new ArrayList();Iterator i = uniqueSet().iterator();while (i.hasNext() Object current
17、 = i.next();for (int index = getCount(current); index > 0; index-) result.add(current);return result;private int modCount() return _mods;public String toString() StringBuffer buf = new StringBuffer(); buf.append("");Iterator i = uniqueSet().iterator();while (i.hasNext() Object current =
18、 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 HashBag() super(new HashMap();public HashBa
19、g(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);public Object first() return (SortedMap)
20、getMap().firstKey();public Object last() return (SortedMap) getMap().lastKey();public Comparator comparator() return (SortedMap) getMap() parator();使用 decorate 模式的 Bag 工具類 public class BagUtils/* * An empty unmodifiable bag.*/public static final Bag EMPTY_BAG =UnmodifiableBag.decorate(new HashBag();
21、/* An empty unmodifiable sorted bag.*/public static final Bag EMPTY_SORTED_BAG =UnmodifiableSortedBag.decorate(new TreeBag();public BagUtils() / 這里按常理不應(yīng)該是 public ,應(yīng)該是 private 才對(duì),作者應(yīng)該有其他地方要用到吧public static Bag synchronizedBag(Bag bag) return SynchronizedBag.decorate(bag);public static Bag unmodifiabl
22、eBag(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 transformedBag(Bag bag, Transformer tra
23、nsformer) return TransformedBag.decorate(bag, transformer); public static SortedBag synchronizedSortedBag(SortedBag bag) return SynchronizedSortedBag.decorate(bag);public static SortedBag unmodifiableSortedBag(SortedBag bag) return UnmodifiableSortedBag.decorate(bag);public static SortedBagpredicatedSortedBag(SortedBag bag, Predicate predic
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度教育機(jī)構(gòu)講師團(tuán)隊(duì)合作協(xié)議
- 公司文員勞動(dòng)協(xié)議
- 全球環(huán)境治理項(xiàng)目資金捐贈(zèng)協(xié)議
- 中國(guó)地理讀后感
- 《數(shù)學(xué)競(jìng)賽題庫(kù)設(shè)計(jì)與復(fù)習(xí)教學(xué)教案》
- 大宗商品貿(mào)易管理流程手冊(cè)
- 委托貸款借款合同
- 農(nóng)產(chǎn)品質(zhì)量安全追溯手冊(cè)
- 互聯(lián)網(wǎng)軟件開發(fā)合同協(xié)議
- 綠化工程承包合同協(xié)議
- sm通道開關(guān)量冗余輸入端子模塊使用說明書
- 位相差和光程差
- 高質(zhì)量SCI論文入門必備從選題到發(fā)表全套課件
- 前列腺癌的診斷與治療課件
- 產(chǎn)品開發(fā)的變更流程
- 氣管鏡科室講課ppt課件(PPT 69頁(yè))
- 蝴蝶豌豆花(課堂PPT)
- 無創(chuàng)呼吸機(jī)的應(yīng)用(飛利浦偉康V60)課件
- 口腔修復(fù)學(xué)-第七章-牙列缺失的全口義齒修復(fù)
- 對(duì)于二氧化碳傳感器的現(xiàn)狀及發(fā)展趨勢(shì)的淺分析
- 麥語言函數(shù)手冊(cè)參考模板
評(píng)論
0/150
提交評(píng)論