Java Basic Knowledge Hardening Collection Framework note 43:set Collection TreeSet stores an integer type of element and traverses

Source: Internet
Author: User
Tags comparable

1. TreeSet class overview:

• ability to sort elements according to a certain rule.

• or sort according to the comparator provided when the set was created

• Depending on the construction method used

2. code example:

1  Packagecn.itcast_05;2 3 ImportJava.util.TreeSet;4 5 /*6 * TreeSet: Ability to sort elements according to a certain rule. 7 * There are two ways of sorting8 * A: Natural sort9 * B: Comparator sortTen  *  One * Features of the TreeSet collection: sort and unique  A  *  - * by observing TreeSet's Add () method , we know that we will eventually look at the put () method of TreeMap .  -  */ the  Public classTreesetdemo { -      Public Static voidMain (string[] args) { -         //To create a collection object -         //sort in natural order +treeset<integer> ts =NewTreeset<integer>(); -  +         //Create elements and add A         //20,18,23,22,17,24,19,18,24 atTs.add (20); -Ts.add (18); -Ts.add (23); -Ts.add (22); -Ts.add (17); -Ts.add (24); inTs.add (19); -Ts.add (18); toTs.add (24); +  -         //Traverse the          for(Integer i:ts) { * System.out.println (i); $         }Panax Notoginseng     } -}

Run the effect as follows:

3. TreeseT guarantees the source parsing of the ordering of elements :

By observing the Add () method of TreeSet, we know that we will eventually look at the put () method of TreeMap.

1 InterfaceCollection {...}2 3 InterfaceSetextendsCollection {...}4 5 InterfaceNavigablemap {6 7 }8 9 classTreeMapImplementsNavigablemap {Ten       Publicv put (K key, V value) { OneEntry<k,v> T =Root; A         if(T = =NULL) { -Compare (key, key);//type (and possibly null) check -  theRoot =NewEntry<> (key, value,NULL); -Size = 1; -modcount++; -             return NULL; +         } -         intCMP; +Entry<k,v>parent; A         //split comparator and comparable paths atcomparator<?Superk> CPR =Comparator; -         if(CPR! =NULL) { -              Do { -Parent =T; -CMP =Cpr.compare (key, T.key); -                 if(CMP < 0) int =T.left; -                 Else if(CMP > 0) tot =T.right; +                 Else -                     returnT.setvalue (value); the} while(t! =NULL); *         } $         Else {Panax Notoginseng             if(Key = =NULL) -                 Throw Newnullpointerexception (); thecomparable<?Superk> k = (comparable<?SuperK>) key; +              Do { AParent =T; theCMP =K.compareto (t.key); +                 if(CMP < 0) -t =T.left; $                 Else if(CMP > 0) $t =T.right; -                 Else -                     returnT.setvalue (value); the} while(t! =NULL); -         }WuyiEntry<k,v> e =NewEntry<>(key, value, parent); the         if(CMP < 0) -Parent.left =e; Wu         Else -Parent.right =e; About fixafterinsertion (e); $size++; -modcount++; -         return NULL; -     } A } +  the classTreeSetImplementsSet { -     Private transientNavigablemap<e,object>m; $      the      PublicTreeSet () { the           This(NewTreemap<e,object>()); the     } the  -      Public BooleanAdd (e e) { in         returnM.put (e, PRESENT) = =NULL; the     } the}

The real comparison is dependent on the CompareTo () method of the element, and this method is defined in the comparable. So, if you want to rewrite the method, you must first comparable the interface. This interface represents a natural sort.

Java Basic Knowledge Hardening Collection Framework note 43:set Collection TreeSet stores an integer type of element and traverses

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.