One Class A day, not a bit tired.
Now it's about---TreeSet.
Treeset<e>extends abstractset<e>implements Navigableset<e>, cloneable, Serializable
To tell you the truth, I haven't used this *_*.
--"a NavigableSet
implementation based on a TreeMap
." The elements is ordered using their natural ordering, or by a Comparator
provided at set creation time, depending on which con Structor is used.
As above, the TreeSet implements the Navigableset, which requires that the elements are sorted in natural order or using the comparator comparer to implement
--"This implementation provides guaranteed log (n) time cost for the basic operations ( add
, and remove
contains
).
Some basic operations like the ones above say their time complexity in log (n)
--"Note that this implementation are notsynchronized
Still, he is non-thread safe.
Thank!! ~ ~ also provides us with a thread-safe way:
SortedSet s = collections.synchronizedsortedset (new TreeSet (...));
"The internal implementation of TreeSet is also implemented using TreeMap"
1. Construction Method:
He offers a number of ways to construct it.
public TreeSet ();
Public TreeSet (COMPARATOR<? super e> Comparator);
Public TreeSet (collection<? extends e> c)
Public TreeSet (sortedset<e> s)
2. iterators
Do you remember that LinkedList provides an inverted iterator. The same treeset is also provided.
1 Public Iterator<e> Iterator () {2 return m.navigablekeyset (). Iterator (); 3 }
Since this navigableset m is maintained in TreeSet, the return of all iterators only uses the iterator of his keyset.
3, some special methods of use:
Public E First () {
return M.firstkey ();
} Gets the first element
There is also the last element to get final ()
1 Public e Lower (e e) {2 return M.lowerkey (e); 3 }
Gets the closest element that is smaller than E.
1 Public e Floor (e e) {2 return M.floorkey (e); 3 }
Gets the smallest element greater than or equal to E
The ceiling of the opposite
The opposite of lower is higher.
Want to learn this class first to understand Navigableset
A Class A day, not a bit tired of the TreeSet