Java Improvement (28)------TreeSet
Like HashSet is based on the HashMap implementation, TreeSet is also based on TREEMAP implementations. In the "Java Improvement Chapter (27)-----TreeMap" In the LZ detailed explanation of TREEMAP implementation mechanism, if Sir details read this blog post or more TreeMap have a more detailed understanding, then treeset the realization of you are drinking saliva so simple.
I. Definition of TreeSet
We know that TreeMap is an ordered two-fork tree, so the same treeset is also an orderly, and its function is to provide an ordered set of sets. Through the source we know TreeSet basic abstractset, realize Navigableset, cloneable, serializable interface. Where Abstractset provides a backbone implementation of the Set interface, minimizing the work required to implement this interface. Navigableset is an extension that SortedSet
has a navigation method that reports the closest match to a given search target, which means that it supports a range of navigation methods. For example, find the most matching item to the specified target. Cloneable supports cloning, Serializable supports serialization.
Public class extends Abstractset<e> implements Navigableset<e>, cloneable, java.io.Serializable
Several variables are defined in the TreeSet.
Private transient Navigablemap<e,object> m; // Privatestaticfinalnew Object ();
Its construction method
// The default constructor. Using this constructor, the elements in the TreeSet are arranged in natural order. TreeSet ()// created TreeSet contains collectionextends e> collection) Specify TreeSet comparer Super e> Comparator)// create TreeSet contain set TreeSet (sortedset<e> Set)
Ii. Main methods of TreeSet
1. Add: Adds the specified element to this set (if the element does not already exist in the set).
Public Boolean Add (e e) { return m.put (e, PRESENT) = =null; }
TreeSet's API
BooleanAdd (E object)BooleanAddAll (collection<?extendsE>collection)voidClear () Object clone ()Booleancontains (Object object) E first ()BooleanIsEmpty () e last () e Pollfirst () E Polllast () e lower (e e) e floor (e e) e ceiling (E e) e higher (e e)Booleanremove (Object object)intsize () Comparator<?SuperE>Comparator () Iterator<E>iterator () iterator<E>descendingiterator () SortedSet<E>HeadSet (E end) Navigableset<E>descendingset () Navigableset<E> HeadSet (E end,Booleanendinclusive) SortedSet<E>subset (e start, E end) Navigableset<E> subset (E start,BooleanStartinclusive, E end,Booleanendinclusive) Navigableset<E> Tailset (E start,Booleanstartinclusive) SortedSet<E> Tailset (E start)
Description :
TreeSet is an ordered set set and therefore supports the add, remove, get, and so on methods.
(02) Like Navigableset, the TreeSet navigation method can be broadly divided into two categories, a class when the navigation method to provide element items, return an element, and another class when providing a collection of navigation methods, return a collection.
Lower, floor, ceiling, and higher return elements that are less than, less than or equal to, greater than or equal, greater than the given element, or null if no such element exists.
TreeSet does not support fast random traversal and can only be traversed by iterators!
The TreeSet Traversal test program is as follows:
ImportJava.util.*;/*** @desc TreeSet's TRAVERSAL program * *@authorSkywang * @email [email protected]*/ Public classTreesetiteratortest { Public Static voidMain (string[] args) {TreeSet set=NewTreeSet (); Set.add ("AAA"); Set.add ("AAA"); Set.add ("BBB"); Set.add ("Eee"); Set.add ("DDD"); Set.add ("CCC"); //Sequential Traversal TreeSetAsciteratorthroughiterator (set); //Reverse Traversal TreeSetDesciteratorthroughiterator (set); //traverse TreeSet through For-each. Not recommended! This method requires that the set be converted to an arrayForeachtreeset (set); } //Sequential Traversal TreeSet Public Static voidAsciteratorthroughiterator (TreeSet set) {System.out.print ("\ n----Ascend Iterator----\ n"); for(Iterator iter =set.iterator (); Iter.hasnext ();) {System.out.printf ("ASC:%s\n", Iter.next ()); } } //Reverse Traversal TreeSet Public Static voidDesciteratorthroughiterator (TreeSet set) {System.out.printf ("\ n----descend Iterator----\ n"); for(Iterator iter =set.descendingiterator (); Iter.hasnext ();) System.out.printf ("Desc:%s\n", (String) Iter.next ()); } //traverse TreeSet through For-each. Not recommended! This method requires that the set be converted to an array Private Static voidForeachtreeset (TreeSet set) {System.out.printf ("\ n----for-each----\ n"); String[] Arr= (string[]) Set.toarray (NewString[0]); for(String Str:arr) System.out.printf ("For each:%s\n", str); }}
Third, the last
Because TreeSet is based on treemap implementation, so if we have a certain understanding of treemap, treeset that is a piece of cake, we can see from the source code in TreeSet, the implementation process is very simple, Almost all of the method implementations are based on TreeMap.
Java-treeset Source Code Analysis