与HashSet是基于HashMap实现一样,TreeSet同样是基于TreeMap实现的。在《Java提高篇(二七)-----TreeMap》中LZ详细讲解了TreeMap实现机制,如果客官详情看了这篇博文或者多TreeMap有比较详细的了解,那么TreeSet的实现对您是喝口水那么简单。
我们知道TreeMap是一个有序的二叉树,那么同理TreeSet同样也是一个有序的,它的作用是提供有序的Set集合。通过源码我们知道TreeSet基础AbstractSet,实现NavigableSet、Cloneable、Serializable接口。其中AbstractSet提供Set接口的骨干实现,从而最大限度地减少了实现此接口所需的工作。NavigableSet是扩展的SortedSet,具有了为给定搜索目标报告最接近匹配项的导航方法,这就意味着它支持一系列的导航方法。比如查找与指定目标最匹配项。Cloneable支持克隆,Serializable支持序列化。
publicclassTreeSet<E>extendsAbstractSet<E> implementsNavigableSet<E>,Cloneable,java.io.Serializable同时在TreeSet中定义了如下几个变量。
privatetransientNavigableMap<E,Object>m; //PRESENT会被当做Map的value与key构建成键值对 privatestaticfinalObjectPRESENT=newObject();其构造方法:
//默认构造方法,根据其元素的自然顺序进行排序 publicTreeSet(){ this(newTreeMap<E,Object>()); } </span><spanstyle="color:rgb(0,128,0)">//</span><spanstyle="color:rgb(0,128,0)">构造一个包含指定collection元素的新TreeSet,它按照其元素的自然顺序进行排序。</span> <spanstyle="color:rgb(0,0,255)">public</span>TreeSet(Comparator<?<spanstyle="color:rgb(0,0,255)">super</span>E><spanstyle="color:rgb(0,0,0)">comparator){ </span><spanstyle="color:rgb(0,0,255)">this</span>(<spanstyle="color:rgb(0,0,255)">new</span>TreeMap<><spanstyle="color:rgb(0,0,0)">(comparator)); } </span><spanstyle="color:rgb(0,128,0)">//</span><spanstyle="color:rgb(0,128,0)">构造一个新的空TreeSet,它根据指定比较器进行排序。</span> <spanstyle="color:rgb(0,0,255)">public</span>TreeSet(Collection<?<spanstyle="color:rgb(0,0,255)">extends</span>E><spanstyle="color:rgb(0,0,0)">c){ </span><spanstyle="color:rgb(0,0,255)">this</span><spanstyle="color:rgb(0,0,0)">(); addAll(c); } </span><spanstyle="color:rgb(0,128,0)">//</span><spanstyle="color:rgb(0,128,0)">构造一个与指定有序set具有相同映射关系和相同排序的新TreeSet。</span> <spanstyle="color:rgb(0,0,255)">public</span>TreeSet(SortedSet<E><spanstyle="color:rgb(0,0,0)">s){ </span><spanstyle="color:rgb(0,0,255)">this</span><spanstyle="color:rgb(0,0,0)">(s.comparator()); addAll(s); } TreeSet(NavigableMap</span><E,Object><spanstyle="color:rgb(0,0,0)">m){ </span><spanstyle="color:rgb(0,0,255)">this</span>.m=<spanstyle="color:rgb(0,0,0)">m; }</span></pre>**1、**add:将指定的元素添加到此set(如果该元素尚未存在于set中)。
publicbooleanadd(Ee){ returnm.put(e,PRESENT)==null; }**2、**addAll:将指定collection中的所有元素添加到此set中。
publicbooleanaddAll(Collection<?extendsE>c){ //Uselinear-timeversionifapplicable if(m.size()==0&&c.size()>0&& cinstanceofSortedSet&& minstanceofTreeMap){ SortedSet<?extendsE>set=(SortedSet<?extendsE>)c; TreeMap<E,Object>map=(TreeMap<E,Object>)m; Comparator<?superE>cc=(Comparator<?superE>)set.comparator(); Comparator<?superE>mc=map.comparator(); if(cc==mc||(cc!=null&&cc.equals(mc))){ map.addAllForTreeSet(set,PRESENT); returntrue; } } returnsuper.addAll(c); }**3、**ceiling:返回此set中大于等于给定元素的最小元素;如果不存在这样的元素,则返回null。
publicEceiling(Ee){ returnm.ceilingKey(e); }**4、**clear:移除此set中的所有元素。
publicvoidclear(){ m.clear(); }5、clone:返回TreeSet实例的浅表副本。属于浅拷贝。
publicObjectclone(){ TreeSet<E>clone=null; try{ clone=(TreeSet<E>)super.clone(); }catch(CloneNotSupportedExceptione){ thrownewInternalError(); } clone.m</span>=<spanstyle="color:rgb(0,0,255)">new</span>TreeMap<><spanstyle="color:rgb(0,0,0)">(m); </span><spanstyle="color:rgb(0,0,255)">return</span><spanstyle="color:rgb(0,0,0)">clone; }</span></pre>6、comparator:返回对此set中的元素进行排序的比较器;如果此set使用其元素的自然顺序,则返回null。
publicComparator<?superE>comparator(){ returnm.comparator(); }7、contains:如果此set包含指定的元素,则返回true。
publicbooleancontains(Objecto){ returnm.containsKey(o); }8、descendingIterator:返回在此set元素上按降序进行迭代的迭代器。
publicIterator<E>descendingIterator(){ returnm.descendingKeySet().iterator(); }9、descendingSet:返回此set中所包含元素的逆序视图。
publicNavigableSet<E>descendingSet(){ returnnewTreeSet<>(m.descendingMap()); }10、first:返回此set中当前第一个(最低)元素。
publicEfirst(){ returnm.firstKey(); }11、floor:返回此set中小于等于给定元素的最大元素;如果不存在这样的元素,则返回null。
publicEfloor(Ee){ returnm.floorKey(e); }12、headSet:返回此set的部分视图,其元素严格小于toElement。
publicSortedSet<E>headSet(EtoElement){ returnheadSet(toElement,false); }13、higher:返回此set中严格大于给定元素的最小元素;如果不存在这样的元素,则返回null。
publicEhigher(Ee){ returnm.higherKey(e); }14、isEmpty:如果此set不包含任何元素,则返回true。
publicbooleanisEmpty(){ returnm.isEmpty(); }15、iterator:返回在此set中的元素上按升序进行迭代的迭代器。
publicIterator<E>iterator(){ returnm.navigableKeySet().iterator(); }16、last:返回此set中当前最后一个(最高)元素。
publicElast(){ returnm.lastKey(); }17、lower:返回此set中严格小于给定元素的最大元素;如果不存在这样的元素,则返回null。
publicElower(Ee){ returnm.lowerKey(e); }18、pollFirst:获取并移除第一个(最低)元素;如果此set为空,则返回null。
publicEpollFirst(){ Map.Entry<E,?>e=m.pollFirstEntry(); return(e==null)?null:e.getKey(); }19、pollLast:获取并移除最后一个(最高)元素;如果此set为空,则返回null。
publicEpollLast(){ Map.Entry<E,?>e=m.pollLastEntry(); return(e==null)?null:e.getKey(); }20、remove:将指定的元素从set中移除(如果该元素存在于此set中)。
publicbooleanremove(Objecto){ returnm.remove(o)==PRESENT; }21、size:返回set中的元素数(set的容量)。
publicintsize(){ returnm.size(); }22、subSet:返回此set的部分视图
/** *返回此set的部分视图,其元素范围从fromElement到toElement。 */ publicNavigableSet<E>subSet(EfromElement,booleanfromInclusive, EtoElement,booleantoInclusive){ returnnewTreeSet<>(m.subMap(fromElement,fromInclusive, toElement,toInclusive)); } </span><spanstyle="color:rgb(0,128,0)">/**</span><spanstyle="color:rgb(0,128,0)"> *返回此set的部分视图,其元素从fromElement(包括)到toElement(不包括)。 </span><spanstyle="color:rgb(0,128,0)">*/</span> <spanstyle="color:rgb(0,0,255)">public</span>SortedSet<E><spanstyle="color:rgb(0,0,0)">subSet(EfromElement,EtoElement){ </span><spanstyle="color:rgb(0,0,255)">return</span>subSet(fromElement,<spanstyle="color:rgb(0,0,255)">true</span>,toElement,<spanstyle="color:rgb(0,0,255)">false</span><spanstyle="color:rgb(0,0,0)">); }</span></pre>23、tailSet:返回此set的部分视图
/** *返回此set的部分视图,其元素大于(或等于,如果inclusive为true)fromElement。 */ publicNavigableSet<E>tailSet(EfromElement,booleaninclusive){ returnnewTreeSet<>(m.tailMap(fromElement,inclusive)); } </span><spanstyle="color:rgb(0,128,0)">/**</span><spanstyle="color:rgb(0,128,0)"> *返回此set的部分视图,其元素大于等于fromElement。 </span><spanstyle="color:rgb(0,128,0)">*/</span> <spanstyle="color:rgb(0,0,255)">public</span>SortedSet<E><spanstyle="color:rgb(0,0,0)">tailSet(EfromElement){ </span><spanstyle="color:rgb(0,0,255)">return</span>tailSet(fromElement,<spanstyle="color:rgb(0,0,255)">true</span><spanstyle="color:rgb(0,0,0)">); }</span></pre>由于TreeSet是基于TreeMap实现的,所以如果我们对treeMap有了一定的了解,对TreeSet那是小菜一碟,我们从TreeSet中的源码可以看出,其实现过程非常简单,几乎所有的方法实现全部都是基于TreeMap的。
本文内容总结:一、TreeSet定义,二、TreeSet主要方法,三、最后,
原文链接:https://www.cnblogs.com/chenssy/p/3772661.html