Sub-Interface---TreeMap of the Java Learning Notes map interface

Source: Internet
Author: User

TreeMap, similar to TreeSet, allows you to sort the elements in the collection while preserving the uniqueness of the elements.

It should be noted that comparable (implement interface, remember to overwrite Comparato method), comparator use.

1 ImportJava.util.Iterator;2 ImportJava.util.TreeMap;3 4 Importcn.itcast.p2.bean.Student;5 ImportCn.itcast.p3.comparator.ComparatorByName;6 7  Public classTreemapdemo {8 9      Public Static voidMain (string[] args) {Tentreemap<student,string> TM =NewTreemap<student,string> (Newcomparatorbyname ()); One          ATm.put (NewStudent ("Lisi", 38), "Beijing"); -Tm.put (NewStudent ("Zhaoliu", 24), "Shanghai"); -Tm.put (NewStudent ("Xiaoqiang", 31), "Shenyang"); theTm.put (NewStudent ("Wangcai", 38), "Dalian"); -Tm.put (NewStudent ("Zhaoliu", 24), "Tieling"); -          -Iterator<student> it =Tm.keyset (). iterator (); +          while(It.hasnext ()) -         { +Student key =It.next (); AString value =Tm.get (key); atSystem.out.println (Key.getname () + ":" +key.getage () + "--" +value); -         } -  -  -     } -  in}

Sub-Interface---TreeMap of the Java Learning Notes map interface

Related Article

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.