Java Api--treemap Class

Source: Internet
Author: User

1, TreeMap class overviewKey is a red-black tree structure, which guarantees the ordering and uniqueness of keys 2. TreeMap CaseTreemap<string,string> treemap<student,string> Example 1:
 PackageTreemapdemos;ImportJava.util.Set;ImportJava.util.TreeMap;/*** Created by Gao on 15-12-22.*//** TreeMap: is based on the red-black tree map interface implementation. * hashmap<string,string> * Key: String * Value: String*/ Public classTreeMapDemo01 { Public Static voidMain (string[] args) {//To create a collection objecttreemap<string, String> TM =NewTreemap<string, string>(); //creating elements and adding elementsTm.put ("Hello", "Hello"); Tm.put ("World", "worlds"); Tm.put ("Java", "Javanese"); Tm.put ("World", "2"); Tm.put ("Java ee", "java ee"); //iterating through the collectionSet<string> set =Tm.keyset ();  for(String key:set) {String value=Tm.get (key); SYSTEM.OUT.PRINTLN (Key+ "---" +value); }    }}

Output result: (unique and natural sort)

Hello---hello java---javanese java ee---java eeworld---World 2 Example 2:
 PackageTreemapdemos;ImportJava.util.Comparator;ImportJava.util.Set;ImportJava.util.TreeMap;/*** Created by Gao on 15-12-22.*//** treemap<student,string> * Key: Student * Value: String*/ Public classTreeMapDemo02 { Public Static voidMain (string[] args) {//To create a collection objectTreemap<student, String> TM =NewTreemap<student, String> (NewComparator<student>() {@Override Public intCompare (Student s1, Student S2) {intnum = S1.getage ()-S2.getage (); intnum2 = num = = 0?s1.getname (). CompareTo (S2.getname ()): num; returnnum2;        }        }); //Create student ObjectsStudent S1 =NewStudent ("Koh Phangan", 30); Student S2=NewStudent ("Willow Xia Hui", 35); Student S3=NewStudent ("Tong Pak Fu", 33); Student S4=NewStudent ("燕青", 32); Student S5=NewStudent ("Tong Pak Fu", 33); //Storage ElementsTm.put (S1, "Song Dynasty"); Tm.put (S2,Yuan Dynasty); Tm.put (S3,Ming Dynasty); Tm.put (S4,Qing Dynasty); Tm.put (S5,Han Dynasty); //TraverseSet<student> set =Tm.keyset ();  for(Student key:set) {String value=Tm.get (key); System.out.println (Key.getname ()+ "---" + key.getage () + "---" +value); }    }}
output Result: (unique and sort)Koh Phangan------Song Dynasty 燕青------qing fu pak Tong------Han Dynasty Hui---Yuan Dynasty

Java Api--treemap Class

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.