Public class extends Implements Navigableset<e>, Cloneable, java.io.Serializable
TreeSet implementations are based on TreeMap, and the order of the elements depends on the natural order of the elements or the comparer provided when they are created.
For basic operations, the time complexity of add, remove, and contains is Logn.
is not thread-safe, if it must be synchronized in a multithreaded environment, it can be synchronized with an object as a lock, or using Collections.synchronizedsortedset (new TreeSet (...)); Method is synchronized.
The iterator method is quick to fail, ensuring that errors are found as soon as possible.
Private transient Navigablemap<e,object> m; // Dummy value to associate with a Object in the backing Map Private Static Final New Object ();
TreeSet internal maintenance is a navigablemap, usually a treemap,present is the value of all key-value pairs as a map.
5 constructors:
TreeSet (navigablemap<e,object>m) { This. m =m;} PublicTreeSet () { This(NewTreemap<e,object>());} PublicTreeSet (comparator<?SuperE>comparator) { This(NewTreemap<>(Comparator));} PublicTreeSet (collection<?extendsE>c) { This(); AddAll (c);} PublicTreeSet (sortedset<e>s) { This(S.comparator ()); AddAll (s);}
2 iterators:
// incrementing iterators Public Iterator<e> Iterator () { return m.navigablekeyset (). Iterator (); // Descending iterator Public Iterator<e> Descendingiterator () { return m.descendingkeyset (). Iterator ();
// returns the decremented set Public Navigableset<e> Descendingset () { returnnew treeset<>( M.descendingmap ());}
//Insert succeeded returns True, failed or returned false already exists Public BooleanAdd (e e) {returnM.put (e, PRESENT) = =NULL;}//Delete succeeds returns True, otherwise returns false Public BooleanRemove (Object o) {returnM.remove (o) = =PRESENT;}//determine if the element contains O Public Booleancontains (Object o) {returnM.containskey (o);}
// returns a subset of the collection, with a two Boolean representation of whether the bounds are included Public Boolean frominclusive, E toelement, boolean toinclusive) { returnnew treeset<> (M.submap (fromelement, frominclusive, toelement, toinclusive));} // There are some other headset,tailset, similar to the subset
You can return to the comparer:
Public Super E> Comparator () { return m.comparator ();}
//returns the first element PublicE First () {returnM.firstkey (); }//returns the last element PublicE Last () {returnM.lastkey (); }//returns the largest element that is less than e Publice Lower (e e) {returnM.lowerkey (e); }//returns the largest element less than or equal to E Publice Floor (e e) {returnM.floorkey (e); }//returns the smallest element greater than or equal to E Publice ceiling (e e) {returnM.ceilingkey (e); }//returns the smallest element greater than E Publice higher (e e) {returnM.higherkey (e); }
// Delete and return the first element Public e Pollfirst () { map.entry<E,?> E = m.pollfirstentry (); return NULL NULL : E.getkey ();} // Delete and return the last element Public e polllast () { map.entry<E,?> E = m.polllastentry (); return NULL NULL : E.getkey ();}
Implementing the Clone method
Public Object Clone () { TreeSet<E> clone; Try { super. Clone (); Catch (clonenotsupportedexception e) { thrownew internalerror (e); } New treemap<>(m); return clone; }
Two methods for serialization and deserialization:
Private voidWriteObject (java.io.ObjectOutputStream s)throwsjava.io.IOException {//Write out any hidden stuffS.defaultwriteobject (); //Write out ComparatorS.writeobject (M.comparator ()); //Write out SizeS.writeint (M.size ()); //Write elements in the proper order. for(e e:m.keyset ()) S.writeobject (e); }Private voidReadObject (java.io.ObjectInputStream s)throwsjava.io.IOException, ClassNotFoundException {//Read in any hidden stuffS.defaultreadobject (); //Read in Comparator@SuppressWarnings ("Unchecked") Comparator<?Supere> C = (comparator<?)SuperE>) S.readobject (); //Create backing TreeMaptreemap<e,object> TM =NewTreemap<>(c); M=TM; //Read in size intSize =S.readint (); Tm.readtreeset (size, S, PRESENT); }
Java.util.TreeSet Source Code Analysis