The source code analysis of TreeSet and TreeMap JDK7

Source: Internet
Author: User

The TreeSet storage principle is: Non-repeatable, orderly.

 Public TreeSet () {        this (new treemap<e,object>());    } 
     Public Super E> Comparator) {        this (new treemap<>(Comparator));    } 

These are the two constructors commonly used by TreeSet.

The underlying implementation is also treemap.

TreeSet (navigablemap<e,object> m) {        this. m = m;    } 
    /**      * The backing map.      */    Private transient Navigablemap<e,object> m;     // Dummy value to associate with a Object in the backing Map    Private Static Final New Object ();

The above two variables are maintained.

Add method

 Public Boolean Add (e e) {        return m.put (e, PRESENT) = =null;    }

Because TreeMap stores a key-value pair, he will save the element as a key, with a constant as the value to TreeMap.

This is why the TreeSet saved elements are not repeatable, because the keys of the treemap cannot be duplicated.

To here TreeSet also almost analyzed, mainly or analysis treemap, because realized in him.

TreeMap commonly used constructors:

     Public Super K> Comparator) {        this. Comparator = comparator;    } 
     Public TreeMap () {        null;    }

Here comparator is a comparator, which is why TreeSet is an orderly cause.

    Private Final Super K> Comparator;     Private transient null;

The above two main variables, one is the comparator, the other is the root. This root is a data structure designed for a data structure, a red-black tree or a binary tree. TreeMap is also designed according to this data structure.

OK, let's take a look at the true colors of the added elements.

 Publicv put (K key, V value) {Entry<K,V> t = root;//Get root node        if(T = =NULL) {//Add first elementCompare (key, key);//type (and possibly null) checkRoot=NewEntry<> (key, value,NULL);//The root node is the first element to be added, and the previous node is nullSize = 1; Modcount++; return NULL; }        intCMP; Entry<K,V>parent; //split comparator and comparable pathscomparator<?Superk> CPR =Comparator; if(CPR! =NULL) {//have comparators             Do{Parent= t;//Starting from the root nodeCMP = Cpr.compare (key, T.key);//newly added element and root node comparison                if(CMP < 0)//It's small, let's go left.t =T.left; Else if(CMP > 0)//big, put the right.t =T.right; Else                    returnT.setvalue (value);//return as equals and replace the new element value with the old} while(t! =NULL);//until there are no left and right nodes        }        Else {            if(Key = =NULL)                Throw NewNullPointerException (); Comparable<?Superk> k = (comparable<?SuperK>) key;  Do{Parent=T; CMP= K.compareto (T.key);//Sort By Nature                if(CMP < 0) T=T.left; Else if(CMP > 0) T=T.right; Else                    returnT.setvalue (value); }  while(t! =NULL); } Entry<K,V> e =NewEntry<>(key, value, parent); if(CMP < 0) Parent.left= e;//at this point, the value of the parent element is minimal and the value of the new element is smaller than the parent, left on the parent        ElseParent.right= e;//At this point, the parent element has the largest value and the new element has a larger value than the parent and is placed on the right side of the parentfixafterinsertion (e); Size++; Modcount++; return NULL; }

OK, now that the elements are in, take them out. Use the iterator () method.

// TreeSet  Public Iterator<e> Iterator () {        return  m.navigablekeyset (). Iterator ();    

The iterator method for TreeSet is to invoke the TreeMap method. That's the way it started from JDK1.6.

/**  @since 1.6     */public navigableset<k>           Navigablekeyset () {        KeySet<K> nks = navigablekeyset;         return NULL New KeySet (this));    }

is actually called the iterator method of the keyset.

The source code analysis of TreeSet and TreeMap JDK7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.