Java教程CommonsCollections学习笔记一.docx
- 文档编号:4042898
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:12
- 大小:16.93KB
Java教程CommonsCollections学习笔记一.docx
《Java教程CommonsCollections学习笔记一.docx》由会员分享,可在线阅读,更多相关《Java教程CommonsCollections学习笔记一.docx(12页珍藏版)》请在冰豆网上搜索。
Java教程CommonsCollections学习笔记一
Java教程:
CommonsCollections学习笔记
(一)
publicinterfaceBagextendsCollection
{
intgetCount(Objectobject);
booleanadd(Objectobject);
booleanadd(Objectobject,intnCopies);
booleanremove(Objectobject);
booleanremove(Objectobject,intnCopies);
SetuniqueSet();
intsize();
booleancontainsAll(Collectioncoll);
booleanremoveAll(Collectioncoll);
booleanretainAll(Collectioncoll);
Iteratoriterator();
}
publicinterfaceSortedBagextendsBag
{
publicComparatorcomparator();
publicObjectfirst();
publicObjectlast();
}
publicabstractclassDefaultMapBagimplementsBag
{
privateMap_map=null;//底层数据存储区
privateint_total=0;//元素总个数
privateint_mods=0;//修改次数
publicDefaultMapBag(){
}
protectedDefaultMapBag(Mapmap){
setMap(map);
}
publicbooleanadd(Objectobject){
returnadd(object,1);
}
publicbooleanadd(Objectobject,intnCopies){
_mods++;
if(nCopies>0){
intcount=(nCopies+getCount(object));
_map.put(object,newInteger(count));
_total+=nCopies;
return(count==nCopies);
}else{
returnfalse;
}
}
publicbooleanaddAll(Collectioncoll){
booleanchanged=false;
Iteratori=coll.iterator();
while(i.hasNext()){
booleanadded=add(i.next());
changed=changed||added;
}
returnchanged;
}
publicvoidclear(){
_mods++;
_map.clear();
_total=0;
}
publicbooleancontains(Objectobject){
return_map.containsKey(object);
}
publicbooleancontainsAll(Collectioncoll){
returncontainsAll(newHashBag(coll));
}
publicbooleancontainsAll(Bagother){
booleanresult=true;
Iteratori=other.uniqueSet().iterator();
while(i.hasNext()){
Objectcurrent=i.next();
booleancontains=getCount(current)>=other.getCount(current);
result=result&&contains;
}
returnresult;
}
publicbooleanequals(Objectobject){
if(object==this){
returntrue;
}
if(objectinstanceofBag==false){
returnfalse;
}
Bagother=(Bag)object;
if(other.size()!
=size()){
returnfalse;
}
for(Iteratorit=_map.keySet().iterator();it.hasNext();){
Objectelement=it.next();
if(other.getCount(element)!
=getCount(element)){
returnfalse;
}
}
returntrue;
}
publicinthashCode(){
return_map.hashCode();
}
publicbooleanisEmpty(){
return_map.isEmpty();
}publicIteratoriterator(){
returnnewBagIterator(this,extractList().iterator());
}
staticclassBagIteratorimplementsIterator{
privateDefaultMapBag_parent=null;
privateIterator_support=null;//原始迭代器
privateObject_current=null;//当前元素
privateint_mods=0;
publicBagIterator(DefaultMapBagparent,Iteratorsupport){
_parent=parent;
_support=support;
_current=null;
_mods=parent.modCount();
}
publicbooleanhasNext(){
return_support.hasNext();
}
publicObjectnext(){
if(_parent.modCount()!
=_mods){
thrownewConcurrentModificationException();
}
_current=_support.next();
return_current;
}
publicvoidremove(){
if(_parent.modCount()!
=_mods){
thrownewConcurrentModificationException();
}
_support.remove();
_parent.remove(_current,1);
_mods++;
}
}
publicbooleanremove(Objectobject){
returnremove(object,getCount(object));
}
publicbooleanremove(Objectobject,intnCopies){
_mods++;
booleanresult=false;
intcount=getCount(object);
if(nCopies<=0){
result=false;
}elseif(count>nCopies){
_map.put(object,newInteger(count-nCopies));
result=true;
_total-=nCopies;
}else{//count>0&&count<=i //needtoremoveall
result=(_map.remove(object)!
=null);
_total-=count;
}
returnresult;
}
publicbooleanremoveAll(Collectioncoll){
booleanresult=false;
if(coll!
=null){
Iteratori=coll.iterator();
while(i.hasNext()){
booleanchanged=remove(i.next(),1);
result=result||changed;
}
}
returnresult;
}
publicbooleanretainAll(Collectioncoll){
returnretainAll(newHashBag(coll));
}
publicbooleanretainAll(Bagother){
booleanresult=false;
Bagexcess=newHashBag();
Iteratori=uniqueSet().iterator();
while(i.hasNext()){
Objectcurrent=i.next();
intmyCount=getCount(current);
intotherCount=other.getCount(current);
if(1<=otherCount&&otherCount<=myCount){
excess.add(current,myCount-otherCount);
}else{
excess.add(current,myCount);
}
}
if(!
excess.isEmpty()){
result=removeAll(excess);
}
returnresult;
}
publicObject[]toArray(){
returnextractList().toArray();
}
publicObject[]toArray(Object[]array){
returnextractList().toArray(array);
}
publicintgetCount(Objectobject){
intresult=0;
Integercount=MapUtils.getInteger(_map,object);
if(count!
=null){
result=count.intValue();
}
returnresult;
}
publicSetuniqueSet(){
returnUnmodifiableSet.decorate(_map.keySet());
}
publicintsize(){
return_total;
}
protectedintcalcTotalSize(){
_total=extractList().size();
return_total;
}
protectedvoidsetMap(Mapmap){
if(map==null||map.isEmpty()==false){
thrownewIllegalArgumentException("Themapmustbenon-nullandempty");
}
_map=map;
}
protectedMapgetMap(){
return_map;
}
privateListextractList(){
Listresult=newArrayList();
Iteratori=uniqueSet().iterator();
while(i.hasNext()){
Objectcurrent=i.next();
for(intindex=getCount(current);index>0;index--){
result.add(current);
}
}
returnresult;
}privateintmodCount(){
return_mods;
}
publicStringtoString(){
StringBufferbuf=newStringBuffer();
buf.append("[");
Iteratori=uniqueSet().iterator();
while(i.hasNext()){
Objectcurrent=i.next();
intcount=getCount(current);
buf.append(count);
buf.append(":
");
buf.append(current);
if(i.hasNext()){
buf.append(",");
}
}
buf.append("]");
returnbuf.toString();
}
}
publicclassHashBagextendsDefaultMapBagimplementsBag
{
publicHashBag(){
super(newHashMap());
}
publicHashBag(Collectioncoll){
this();
addAll(coll);
}
}
publicclassTreeBagextendsDefaultMapBagimplementsSortedBag
{
publicTreeBag(){
super(newTreeMap());
}
publicTreeBag(Comparatorcomparator){
super(newTreeMap(comparator));
}
publicTreeBag(Collectioncoll){
this();
addAll(coll);
}
publicObjectfirst(){
return((SortedMap)getMap()).firstKey();
}
publicObjectlast(){
return((SortedMap)getMap()).lastKey();
}
publicComparatorcomparator(){
return((SortedMap)getMap()).comparator();
}
}
使用decorate模式的Bag工具类
publicclassBagUtils
{
/**
*Anemptyunmodifiablebag.
*/
publicstaticfinalBagEMPTY_BAG=UnmodifiableBag.decorate(newHashBag());
/**
*Anemptyunmodifiablesortedbag.
*/
publicstaticfinalBagEMPTY_SORTED_BAG=UnmodifiableSortedBag.decorate(newTreeBag());
publicBagUtils(){//这里按常理不应该是public,应该是private才对,作者应该有其他地方要用到吧
}
publicstaticBagsynchronizedBag(Bagbag){
returnSynchronizedBag.decorate(bag);
}
publicstaticBagunmodifiableBag(Bagbag){
returnUnmodifiableBag.decorate(bag);
}
publicstaticBagpredicatedBag(Bagbag,Predicatepredicate){
returnPredicatedBag.decorate(bag,predicate);
}
publicstaticBagtypedBag(Bagbag,Classtype){
returnTypedBag.decorate(bag,type);
}
publicstaticBagtransformedBag(Bagbag,Transformertransformer){
returnTransformedBag.decorate(bag,transformer);
}
publicstaticSortedBagsynchronizedSortedBag(SortedBagbag){
returnSynchronizedSortedBag.decorate(bag);
}
publicstaticSortedBagunmodifiableSortedBag(SortedBagbag){
returnUnmodifiableSortedBag.decorate(bag);
}
publicstaticSortedBagpredicatedSortedBag(SortedBagbag,Predicatepredicate){
returnPredicatedSortedBag.decorate(bag,predicate);
}
publicstaticSortedBagtypedSortedBag(SortedBagbag,Classtype){
returnTypedSortedBag.decorate(bag,type);
}
publicstaticSortedBagtransformedSortedBag(SortedBagbag,Transformertransformer){
returnTransformedSortedBag.decorate(bag,transformer);
}
}
【
Word是学生和职场人士最常用的一款办公软件之一,99.99%的人知道它,但其实,这个软件背后,还有一大批隐藏技能你不知道。
掌握他们,你将开启新世界的大门。
Tab+Enter,在编过号以后,会自动编号段落
Ctrl+D调出字体栏,配合Tab+Enter全键盘操作吧
Ctrl+L左对齐,Ctrl+R右对齐,Ctrl+E居中
Ctrl+F查找,Ctrl+H替换。
然后关于替换,里面又大有学问!
有时候Word文档中有许多多余的空行需要删除,这个时候我们可以完全可以用“查找替换”来轻松解决。
打开“编辑”菜单中的“替换”对话框,把光标定位在“查找内容”输入框中,单击“高级”按钮,选择“特殊字符”中的“段落标记”两次,在输入框中会显示“^P^P”,然后在“替换为”输入框中用上面的方法插入一个“段落标记”(一个“^P”),再按下“全部替换”按钮。
这样多余的空行就会被删除。
Ctrl+Z是撤销,那还原呢?
就是Ctrl+Y,撤销上一步撤销!
比如我输入abc,按一下F4,就会自动再输入一遍abc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 教程 CommonsCollections 学习 笔记