版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(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; / 元素總個(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(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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 14《故都的秋》《荷塘月色》對比閱讀說課稿 2024-2025學(xué)年統(tǒng)編版高中語文必修上冊
- 8《網(wǎng)絡(luò)新世界》(說課稿)-部編版道德與法治四年級上冊001
- 9《這些是大家的》說課稿-2023-2024學(xué)年道德與法治二年級上冊統(tǒng)編版
- Unit 1 Back to School Reading 說課稿-2024-2025學(xué)年高一英語譯林版(2020)必修第一冊
- 2024-2025學(xué)年高中歷史 第四單元 工業(yè)文明沖擊下的改革 第15課 戊戌變法(2)教學(xué)說課稿 岳麓版選修1
- 2025市場門市部租賃合同
- 2025電腦維修合同范本
- 2024-2025學(xué)年新教材高中語文 第六單元 10.1 勸學(xué)說課稿(3)部編版必修上冊
- 2025蘋果購銷合同樣書
- 24 京劇趣談(說課稿)-2024-2025學(xué)年統(tǒng)編版語文六年級上冊
- 2025屆上海交大南洋中學(xué)語文高三第一學(xué)期期末學(xué)業(yè)質(zhì)量監(jiān)測試題含解析
- 環(huán)保局社會管理創(chuàng)新方案策劃方案
- 主題二任務(wù)二 《探究身邊信息技術(shù)的奧秘》 教學(xué)設(shè)計(jì) 2023-2024學(xué)年桂科版初中信息技術(shù)七年級上冊
- 人教八年級上冊英語第一單元《Section A (1a-2d)》教學(xué)課件
- 2023年版《安寧療護(hù)實(shí)踐指南(試行)》解讀課件
- 10kV環(huán)網(wǎng)柜改造工程施工方案設(shè)計(jì)
- 電工班三級安全教育內(nèi)容范本
- 中國血管通路專家共識解讀
- 新生兒疾病篩查可疑陽性、陽性兒復(fù)查隨訪登記表
- 開學(xué)前幼兒園安全培訓(xùn)
- 2023年湛江市麻章區(qū)教育局招聘事業(yè)編制教師考試真題
評論
0/150
提交評論